Skip to content

Commit

Permalink
fix: Optimize the code
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong committed Jul 29, 2020
1 parent bf9a489 commit c83dd01
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
windows: `Alt + insert`, Mac: `control + Enter`

#### 最近更新
* 2018.11.2
* 1.4.3 优化了InsertClass的首次插入操作。
* 2020.07.20
* 1.4.4 优化代码

#### 功能介绍
##### 简体中文
Expand Down Expand Up @@ -65,6 +65,7 @@ QQ:11563928
* 1.4.1 修复没有style标签异常的情况,优化了实现缩小了插件大小。
* 1.4.2 修改开源项目地址。
* 1.4.3 优化了InsertClass的首次插入操作。
* 1.4.4 优化代码

#### 给我买杯咖啡 Buy me a coffee
![image](https://user-images.githubusercontent.com/13230237/35954042-88d45846-0cc2-11e8-98a3-29adb4f0be9a.png)
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/InsertAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ public void actionPerformed(AnActionEvent anActionEvent) {
String text = null;//从数据中获取文本值
try {
text = (String) content.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
} catch (UnsupportedFlavorException | IOException e) {
e.printStackTrace();
}
//获取位置
Expand All @@ -42,15 +40,15 @@ public void actionPerformed(AnActionEvent anActionEvent) {
//获取空格数
CaretModel caretModel = editor.getCaretModel();
caretModel.moveToOffset(start);
String space = "";
StringBuilder space = new StringBuilder();
for (int i = 0; i < caretModel.getLogicalPosition().column; i++) {
space += " ";
space.append(" ");
}
if (text == null) {
return;
}
//格式化代码
final String resultText = formatCode(text, space);
final String resultText = formatCode(text, space.toString());
//插入内容
WriteCommandAction.runWriteCommandAction(editor.getProject(),
() -> document.replaceString(start, end, resultText));
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/InsertClassAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@ public void actionPerformed(AnActionEvent anActionEvent) {
int newEndTag = endTag;
String insertString = preInsertString;
WriteCommandAction.runWriteCommandAction(editor.getProject(), () -> document.insertString(newEndTag, insertString));
assert currentPsiFile.getParent() != null;
PsiFile targetPsiFile = currentPsiFile.getParent().findFile(currentFileName + ".vue");
assert targetPsiFile != null;
OpenFileDescriptor openFileDescriptor = new OpenFileDescriptor(
anActionEvent.getProject(),
targetPsiFile.getVirtualFile(),
document.getLineNumber(endTag - 1));
Editor targetEdit = FileEditorManager.getInstance(anActionEvent.getProject())
.openTextEditor(openFileDescriptor, true);
assert targetEdit != null;
ScrollingModel scrollingModel = targetEdit.getScrollingModel();
scrollingModel.scrollTo(targetEdit.offsetToLogicalPosition(endTag + insertString.length() - 1), ScrollType.MAKE_VISIBLE);
CaretModel caretModel = targetEdit.getCaretModel();
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
<change-notes><![CDATA[
<ul>
<li>......</li>
<li>1.3.0 GoCss加入一直期待支持的 Navigate Back & Forward 快捷键来回切换。</li>
<li>1.4.0 新增InsertClass功能,该功能处于公共测试阶段,如有问题,欢迎在github上提出。</li>
<li>1.4.1 修复没有style标签异常的情况,优化了实现缩小了插件大小。</li>
<li>1.4.2 修改开源项目地址。</li>
<li>1.4.3 优化了InsertClass的首次插入操作。</li>
<li>1.4.4 优化代码</li>
</ul>
]]>
</change-notes>
Expand Down

0 comments on commit c83dd01

Please sign in to comment.