Skip to content

Commit e11c20e

Browse files
committed
!1 merge dev to master 1.3.0
* 添加替换提示 * 设置版本号以及版本描述
1 parent 54d5399 commit e11c20e

File tree

11 files changed

+327
-145
lines changed

11 files changed

+327
-145
lines changed

build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55

66

77
group 'com.haojiyou'
8-
version '1.2.0'
8+
version '1.3.0'
99

1010
sourceCompatibility = '11'
1111
targetCompatibility = '11'
@@ -27,10 +27,12 @@ intellij {
2727
patchPluginXml {
2828
changeNotes = """
2929
<em>
30+
<h1> 1.2.0 </h1>
31+
1.增加了替换提示。
32+
3033
<h1> 1.2.0 </h1>
3134
1.增加了撤回功能。
3235
33-
3436
<h1> 1.1.0 </h1>
3537
1.理论上支持常见语言的注释区域不更改。
3638
2.不支持的语言待测试。
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.haojiyou.cnchar;
2+
3+
import com.haojiyou.cnchar.action.CharAutoReplaceAction;
4+
import com.haojiyou.cnchar.common.ReplaceCharConfig;
5+
import com.haojiyou.cnchar.handler.ChineseCharCheckHandler;
6+
import com.intellij.openapi.application.ApplicationManager;
7+
import com.intellij.openapi.command.CommandProcessor;
8+
import com.intellij.openapi.command.WriteCommandAction;
9+
import com.intellij.openapi.diagnostic.Logger;
10+
import com.intellij.openapi.editor.Document;
11+
import com.intellij.openapi.editor.Editor;
12+
import com.intellij.openapi.editor.event.DocumentEvent;
13+
import com.intellij.openapi.editor.event.DocumentListener;
14+
import com.intellij.openapi.editor.impl.EditorImpl;
15+
import com.intellij.openapi.project.Project;
16+
import org.apache.commons.lang3.StringUtils;
17+
import org.jetbrains.annotations.NotNull;
18+
19+
/**
20+
* 描述: 实现DocumentListener接口,进行字符替换处理
21+
*
22+
* @author : best.xu
23+
*/
24+
public class CharTypedDocumentLisener implements DocumentListener {
25+
private static final Logger LOG = Logger.getInstance(CharTypedDocumentLisener.class);
26+
27+
private Editor editor;
28+
29+
30+
public CharTypedDocumentLisener(Editor editor){
31+
this.editor = editor;
32+
}
33+
34+
35+
@Override
36+
public void beforeDocumentChange(@NotNull DocumentEvent event) {
37+
DocumentListener.super.beforeDocumentChange(event);
38+
}
39+
40+
@Override
41+
public void documentChanged(@NotNull DocumentEvent event) {
42+
this.documentChanged(event, this.editor);
43+
}
44+
45+
public void documentChanged(@NotNull DocumentEvent event, Editor editor) {
46+
if (editor == null){
47+
LOG.info("editor is null");
48+
return;
49+
}
50+
if (event.getNewLength() > 5 || !(editor instanceof EditorImpl)){
51+
//输入长度大于5,不处理
52+
LOG.info("input char length > 5");
53+
return;
54+
}
55+
56+
String originalText = event.getNewFragment().toString();
57+
if (StringUtils.isBlank(originalText)){
58+
LOG.info("originalText is blank! return ");
59+
return;
60+
}
61+
62+
String replacement = ReplaceCharConfig.cnCharMap.get(originalText);
63+
if (StringUtils.isBlank(replacement)) {
64+
//没有找到映射的值就不转换了
65+
LOG.info("replacement is blank! return ");
66+
return;
67+
}
68+
69+
CharAutoReplaceAction.INSTANCE.replace(event,editor,originalText,replacement);
70+
71+
event.getDocument().removeDocumentListener(CharTypedDocumentLisener.this);
72+
73+
}
74+
75+
76+
@Override
77+
public void bulkUpdateStarting(@NotNull Document document) {
78+
DocumentListener.super.bulkUpdateStarting(document);
79+
}
80+
81+
@Override
82+
public void bulkUpdateFinished(@NotNull Document document) {
83+
DocumentListener.super.bulkUpdateFinished(document);
84+
}
85+
}

src/main/java/com/haojiyou/cnchar/MyDocumentLisener.java

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.haojiyou.cnchar.action;
2+
3+
import com.haojiyou.cnchar.service.HintService;
4+
import com.intellij.openapi.application.ApplicationManager;
5+
import com.intellij.openapi.command.WriteCommandAction;
6+
import com.intellij.openapi.diagnostic.Logger;
7+
import com.intellij.openapi.editor.Document;
8+
import com.intellij.openapi.editor.Editor;
9+
import com.intellij.openapi.editor.event.DocumentEvent;
10+
import com.intellij.openapi.editor.impl.EditorImpl;
11+
import com.intellij.openapi.project.Project;
12+
import org.jetbrains.annotations.NotNull;
13+
14+
/**
15+
* 描述: 字符自动替换
16+
*
17+
* @author : best.xu
18+
*/
19+
public class CharAutoReplaceAction {
20+
private static final Logger LOG = Logger.getInstance(CharAutoReplaceAction.class);
21+
22+
public static CharAutoReplaceAction INSTANCE = new CharAutoReplaceAction();
23+
24+
public synchronized void replace(@NotNull DocumentEvent event, Editor editor,String originalText, String replacement){
25+
Document document = event.getDocument();
26+
Project project = editor.getProject();
27+
int currentOffset = event.getOffset() + event.getNewLength();
28+
29+
ApplicationManager.getApplication().invokeLater(()-> {
30+
if (editor.isDisposed()) {
31+
LOG.info("editor is disposed, project:"+editor.getProject().getName());
32+
return;
33+
}
34+
WriteCommandAction.runWriteCommandAction(project, new Runnable() {
35+
@Override
36+
public void run() {
37+
document.replaceString(event.getOffset(), currentOffset, replacement);
38+
LOG.info("input char: "+ originalText +" replaced: " + replacement);
39+
HintService.getInstance().showHint((EditorImpl) editor, HintService.INSTANCE.createHint(originalText,
40+
replacement),null);
41+
}
42+
});
43+
});
44+
45+
}
46+
47+
public synchronized void replace2(@NotNull DocumentEvent event, Editor editor,String originalText, String replacement){
48+
Document document = event.getDocument();
49+
Project project = editor.getProject();
50+
int currentOffset = event.getOffset() + event.getNewLength();
51+
Runnable replaceTask = new Runnable() {
52+
@Override
53+
public void run() {
54+
LOG.info("input char: "+ originalText +" replaced: " + replacement);
55+
document.replaceString(event.getOffset(), currentOffset, replacement);
56+
HintService.getInstance().showHint((EditorImpl) editor, HintService.INSTANCE.createHint(originalText,
57+
replacement),null);
58+
}
59+
};
60+
61+
ApplicationManager.getApplication().invokeAndWait(() -> {
62+
if (editor.isDisposed()) {
63+
LOG.info("editor is disposed, project:"+editor.getProject().getName());
64+
return;
65+
}
66+
WriteCommandAction.runWriteCommandAction(project, replaceTask);
67+
});
68+
69+
70+
}
71+
}

src/main/java/com/haojiyou/cnchar/action/EditorHandlerIllustration.java

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/main/java/com/haojiyou/cnchar/common/CommentUtil.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.haojiyou.cnchar.common;
22

3+
import com.intellij.openapi.editor.Document;
4+
import com.intellij.openapi.fileEditor.FileEditorManager;
5+
import com.intellij.openapi.project.Project;
36
import org.apache.commons.lang3.StringUtils;
47

58
import java.util.HashMap;
@@ -16,22 +19,22 @@ public class CommentUtil {
1619
private static final Map<String, String[]> COMMENT_END_MAP = new HashMap<>();
1720

1821
static {
19-
COMMENT_START_MAP.put(FileType.JAVA.getType(), new String[]{"//", "/*", "*"});
20-
COMMENT_END_MAP.put(FileType.JAVA.getType(), new String[]{"*/"});
21-
COMMENT_START_MAP.put(FileType.JS.getType(), new String[]{"//", "/*", "*"});
22-
COMMENT_END_MAP.put(FileType.JS.getType(), new String[]{"*/"});
22+
COMMENT_START_MAP.put(SupportFileType.JAVA.getType(), new String[]{"//", "/*", "*"});
23+
COMMENT_END_MAP.put(SupportFileType.JAVA.getType(), new String[]{"*/"});
24+
COMMENT_START_MAP.put(SupportFileType.JS.getType(), new String[]{"//", "/*", "*"});
25+
COMMENT_END_MAP.put(SupportFileType.JS.getType(), new String[]{"*/"});
2326

24-
COMMENT_START_MAP.put(FileType.SQL.getType(), new String[]{"--", "/*"});
25-
COMMENT_END_MAP.put(FileType.SQL.getType(), new String[]{"*/"});
27+
COMMENT_START_MAP.put(SupportFileType.SQL.getType(), new String[]{"--", "/*"});
28+
COMMENT_END_MAP.put(SupportFileType.SQL.getType(), new String[]{"*/"});
2629

2730

28-
COMMENT_START_MAP.put(FileType.GIT_IGNORE.getType(), new String[] {"#" });
29-
COMMENT_START_MAP.put(FileType.MARKDOWN.getType(), new String[] {"#","--" });
31+
COMMENT_START_MAP.put(SupportFileType.GIT_IGNORE.getType(), new String[] {"#" });
32+
COMMENT_START_MAP.put(SupportFileType.MARKDOWN.getType(), new String[] {"#","--" });
3033

3134
}
3235

3336

34-
public static boolean isComment(String line, FileType fileType) {
37+
public static boolean isComment(String line, SupportFileType fileType) {
3538
if (fileType == null) {
3639
//不支持的文件类型,目前暂不转换
3740
return true;
@@ -49,4 +52,23 @@ public static boolean isComment(String line, FileType fileType) {
4952
return result;
5053
}
5154

55+
/**
56+
* 是否是自定义注释
57+
* @param document
58+
* @param project
59+
* @param currentOffset 当前光标位置
60+
* @return
61+
*/
62+
public static boolean isCustomComment(Document document, Project project, int currentOffset) {
63+
//当前行文本
64+
String currentLineText = DocumentUtil.getLineText(document, currentOffset);
65+
//文件扩展名
66+
String extension = FileEditorManager.getInstance(project).getSelectedEditor().getFile().getExtension();
67+
return isComment(currentLineText, SupportFileType.getFileType(extension));
68+
69+
}
70+
71+
72+
73+
5274
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.haojiyou.cnchar.common;
2+
3+
import com.intellij.openapi.editor.Document;
4+
import com.intellij.openapi.util.TextRange;
5+
6+
/**
7+
* 描述:
8+
*
9+
* @author : lixuran
10+
*/
11+
public class DocumentUtil {
12+
13+
/**
14+
* 获取当前光标行文本
15+
* @param document
16+
* @param currentOffset 当前光标位置
17+
* @return
18+
*/
19+
public static String getLineText(Document document, int currentOffset){
20+
//当前行号
21+
int currentLineNumber = document.getLineNumber(currentOffset);
22+
//当前行开始、结束光标
23+
int currentLineStartOffset = document.getLineStartOffset(currentLineNumber);
24+
int currentLineEndOffset = document.getLineEndOffset(currentLineNumber);
25+
return document.getText(TextRange.create(currentLineStartOffset, currentLineEndOffset));
26+
}
27+
}

src/main/java/com/haojiyou/cnchar/common/FileType.java renamed to src/main/java/com/haojiyou/cnchar/common/SupportFileType.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
* @author : lixuran
99
*/
10-
public enum FileType {
10+
public enum SupportFileType {
1111
JAVA("java"),
1212
XML("xml"),
1313
SQL("sql"),
@@ -21,20 +21,20 @@ public enum FileType {
2121

2222
private String type;
2323

24-
FileType(String type) {
24+
SupportFileType(String type) {
2525
this.type = type;
2626
}
2727

2828
public String getType() {
2929
return type;
3030
}
3131

32-
public static FileType getFileType(String fileExtension){
32+
public static SupportFileType getFileType(String fileExtension){
3333
if (fileExtension == null) {
3434
return null;
3535
}
3636

37-
for (FileType type : FileType.values()){
37+
for (SupportFileType type : SupportFileType.values()){
3838
if (StringUtils.equalsIgnoreCase(fileExtension, type.type )){
3939
return type;
4040
}

0 commit comments

Comments
 (0)