Skip to content

Commit

Permalink
Enabled formatting of selected text.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenichi Hoshi committed Apr 12, 2017
1 parent 14c212a commit 6921f48
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
out
idea-uroborosql-formatter.zip
idea-uroborosql-formatter*.zip
2 changes: 1 addition & 1 deletion Readme.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ How to Use
----------

- Codeメニューから「Format SQL」を選択
- Alt + Shift + L on Windows, Cmd + Shift + L on OS X
- Alt + Shift + L on Windows, Cmd + Ctrl + L on OS X

License
-------
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ How to Use
----------

- Select "Format SQL" from the Code menu
- Use Alt + Shift + L on Windows, Cmd + Shift + L on OS X to format currently active document.</p>
- Use Alt + Shift + L on Windows, Cmd + Ctrl + L on OS X to format currently active document.</p>

License
-------
Expand Down
17 changes: 11 additions & 6 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<idea-plugin>
<id>jp.co.future.ideausqlfmt</id>
<name>IntelliJ uroboroSQL Formatter</name>
<version>0.1.1</version>
<version>0.2.0</version>
<vendor email="[email protected]" url="https://github.com/future-architect/idea-uroborosql-formatter">Future
Architect, Inc.
</vendor>
Expand All @@ -25,7 +25,7 @@
<h2>How to use</h2>
<p>Use Alt + Shift + L on Windows, Cmd + Shift + L on OS X to format currently active document.</p>
<p>Use Alt + Shift + L on Windows, Cmd + Ctrl + L on OS X to format currently active document.</p>
<p>The following options can be selected by the Preferences dialog.</p>
<ul>
<li>Convert reserved words and identifiers to upper case</li>
Expand All @@ -35,13 +35,18 @@
]]></description>

<change-notes><![CDATA[
<h2>0.2 (2017/04/12)</h2>
<ul>
<li>Enabled formatting of selected text.</li>
<li>Change default keyboard shortcut.</li>
</ul>
<h2>0.1.1 (2017/04/08)</h2>
<ul>
<li>change default keyboard shortcut</li>
<li>Change default keyboard shortcut.</li>
</ul>
<h2>0.1.0 (2017/04/06)</h2>
<ul>
<li>First release</li>
<li>First release.</li>
</ul>
]]>
</change-notes>
Expand All @@ -61,8 +66,8 @@
<add-to-group group-id="CodeFormatGroup" anchor="first"/>

<keyboard-shortcut keymap="$default" first-keystroke="alt shift L"/>
<keyboard-shortcut keymap="Mac OS X" first-keystroke="meta shift L"/>
<keyboard-shortcut keymap="Mac OS X 10.5+" first-keystroke="meta shift L"/>
<keyboard-shortcut keymap="Mac OS X" first-keystroke="meta ctrl L"/>
<keyboard-shortcut keymap="Mac OS X 10.5+" first-keystroke="meta ctrl L"/>
</action>
</actions>

Expand Down
21 changes: 19 additions & 2 deletions src/java/jp/co/future/ideausqlfmt/UroborosqlFormatterAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.intellij.notification.*;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.application.*;
import com.intellij.openapi.command.*;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.project.*;
import jp.co.future.ideausqlfmt.python.*;

/**
Expand All @@ -20,7 +22,13 @@ public class UroborosqlFormatterAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent e) {
Editor editor = e.getData(PlatformDataKeys.EDITOR);
String text = editor.getDocument().getText();
Project project = e.getData(PlatformDataKeys.PROJECT);

SelectionModel selectionModel = editor.getSelectionModel();
String text = selectionModel.getSelectedText();
if (text == null) {
text = editor.getDocument().getText();
}
engine.put("sql", text);

// bind python config
Expand All @@ -33,7 +41,16 @@ public void actionPerformed(AnActionEvent e) {
String fmtText = engine.get("f");

// bind editor
ApplicationManager.getApplication().runWriteAction(() -> editor.getDocument().setText(fmtText));
if (selectionModel.getSelectedText() == null){
ApplicationManager.getApplication().runWriteAction(() -> {
editor.getDocument().setText(fmtText);
});
} else {
WriteCommandAction.runWriteCommandAction(project, () -> {
editor.getDocument().replaceString(selectionModel.getSelectionStart(),
selectionModel.getSelectionEnd(), fmtText);
});
}

Notifications.Bus.notify(
new Notification("uroborosql", "Success",
Expand Down

0 comments on commit 6921f48

Please sign in to comment.