Skip to content

Commit e5e4d5e

Browse files
committed
fixed bugs
1 parent b37e6cd commit e5e4d5e

File tree

2 files changed

+89
-34
lines changed

2 files changed

+89
-34
lines changed

deepcommenter-client/src/tech/czxs/deepcommenter/GenerateCommentAction.java

Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package tech.czxs.deepcommenter;
22

3+
import com.intellij.codeInsight.hint.HintManager;
34
import com.intellij.openapi.actionSystem.AnAction;
45
import com.intellij.openapi.actionSystem.AnActionEvent;
56
import com.intellij.openapi.actionSystem.PlatformDataKeys;
@@ -15,8 +16,6 @@
1516
import com.intellij.openapi.util.TextRange;
1617
import org.jetbrains.annotations.NotNull;
1718

18-
import java.io.InputStream;
19-
import java.io.InputStreamReader;
2019
import java.util.Properties;
2120

2221
public class GenerateCommentAction extends AnAction {
@@ -31,6 +30,30 @@ public void actionPerformed(AnActionEvent e) {
3130
SelectionModel selectionModel = editor.getSelectionModel();
3231
String selectedText = selectionModel.getSelectedText();
3332
if (selectedText == null) return;
33+
int lineBreakCount = 0;
34+
for (int i = 0; i < selectedText.length(); i++) {
35+
if (selectedText.charAt(i) == '\n') lineBreakCount++;
36+
}
37+
if (lineBreakCount > 50) {
38+
HintManager.getInstance().showErrorHint(editor, "The method content is too long.");
39+
return;
40+
}
41+
42+
Document document = editor.getDocument();
43+
44+
int selectionStart = selectionModel.getSelectionStart();
45+
int selectionEnd = selectionModel.getSelectionEnd();
46+
String docString = document.getText();
47+
String s = docString.substring(selectionStart, selectionEnd);
48+
int start = getKeywordIndex(s);
49+
if (start == -1) {
50+
selectionStart = getNearestKeywordIndex(docString, selectionStart);
51+
} else {
52+
selectionStart += start;
53+
}
54+
selectionEnd = getMethodEndIndex(docString.substring(selectionStart));
55+
selectionEnd += selectionStart;
56+
selectedText = docString.substring(selectionStart, selectionEnd + 1);
3457

3558
ProgressManager.getInstance().run(
3659
new Task.Modal(project, "Generating Comment", true) {
@@ -46,19 +69,14 @@ public void run(@NotNull ProgressIndicator indicator) {
4669

4770
}
4871
});
49-
// try {
50-
// Thread.sleep(5000);
51-
// } catch (InterruptedException ex) {
52-
// ex.printStackTrace();
53-
// }
72+
5473
String result;
5574
try {
5675
Properties props = new Properties();
5776
props.load(this.getClass().getResourceAsStream("/server.properties"));
5877
String serverAddr = props.getProperty("server-address");
5978

60-
String s = selectedText;
61-
result = HttpClientPool.getHttpClient().post("http://"+ serverAddr + ":5000/s", s);
79+
result = HttpClientPool.getHttpClient().post("http://" + serverAddr + ":5000/s", selectedText);
6280
// result = HttpClientPool.getHttpClient().post("http://127.0.0.1:5000/s", s);
6381

6482
} catch (Exception ex) {
@@ -67,38 +85,90 @@ public void run(@NotNull ProgressIndicator indicator) {
6785
return;
6886
}
6987

70-
Document document = editor.getDocument();
71-
72-
73-
int selectionStart = selectionModel.getSelectionStart();
7488
int line = 0;
7589
int lineOffset = 0;
7690
for (int i = 0; i < document.getLineCount(); i++) {
7791
int tmpOffset = document.getLineStartOffset(i);
78-
if(tmpOffset <= selectionStart) {
92+
if (tmpOffset <= selectionStart) {
7993
line = i;
8094
lineOffset = tmpOffset;
8195
} else break;
8296
}
8397
int lineEndOffset = document.getLineEndOffset(line);
8498

85-
String s = document.getText(new TextRange(lineOffset, lineEndOffset));
99+
s = document.getText(new TextRange(lineOffset, lineEndOffset));
86100

87101
int spaceNum = 0;
88102
for (int i = 0; i < s.length(); i++) {
89-
if(s.charAt(i) == ' ') spaceNum++;
103+
if (s.charAt(i) == ' ') spaceNum++;
90104
else break;
91105
}
92106
StringBuilder sb = new StringBuilder();
93107
for (int i = 0; i < spaceNum; i++) sb.append(" ");
94108

95109
final int insertOffset = lineOffset;
96-
final String result1 = result;
110+
final String result1 = sb + "/**\n" + sb + " * " + result + sb + " */\n";
97111
WriteCommandAction.runWriteCommandAction(project, () ->
98-
document.insertString(insertOffset, sb + "/**\n" + sb + " * " + result1 + sb + " */\n"));
112+
document.insertString(insertOffset, result1));
113+
114+
selectionStart += result1.length();
115+
selectionEnd += result1.length();
116+
selectionModel.setSelection(selectionStart, selectionEnd + 1);
117+
118+
}
119+
120+
public static int getNearestKeywordIndex(String s, int idx) {
121+
for (int i = idx; i >= 0; i--) {
122+
if(s.charAt(i) == 'p') {
123+
if(s.substring(i).startsWith("public")) return i;
124+
if(s.substring(i).startsWith("private")) return i;
125+
if(s.substring(i).startsWith("protected")) return i;
126+
}
127+
}
128+
return 0;
129+
}
99130

100-
selectionModel.removeSelection();
131+
public static int getKeywordIndex(String s) {
132+
boolean stringBlind = false;
133+
for (int i = 0; i < s.length(); i++) {
134+
if(s.charAt(i) == '"') {
135+
if(i == 0 || s.charAt(i-1) != '\\')
136+
stringBlind = !stringBlind;
137+
}
138+
if(stringBlind) continue;
139+
if(s.charAt(i) == 'p') {
140+
if(s.substring(i).startsWith("public")) return i;
141+
if(s.substring(i).startsWith("private")) return i;
142+
if(s.substring(i).startsWith("protected")) return i;
143+
}
144+
}
145+
return -1;
146+
}
101147

148+
public static int getMethodEndIndex(String s) {
149+
int endIdx = s.length() - 1;
150+
int bracketCount = 0;
151+
boolean flag = false;
152+
boolean stringBlind = false;
153+
for (int i = 0; i < s.length(); i++) {
154+
if(s.charAt(i) == '"') {
155+
if(i == 0 || s.charAt(i-1) != '\\')
156+
stringBlind = !stringBlind;
157+
}
158+
if(stringBlind) continue;
159+
char c = s.charAt(i);
160+
if(c == '{') {
161+
bracketCount++;
162+
flag = true;
163+
} else if(c == '}' && bracketCount > 0) {
164+
bracketCount--;
165+
}
166+
if(flag && bracketCount == 0) {
167+
endIdx = i;
168+
break;
169+
}
170+
}
171+
return endIdx;
102172
}
103173

104174
}

deepcommenter-client/src/tech/czxs/deepcommenter/HttpClientPool.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ public class HttpClientPool {
2525
private static PoolingHttpClientConnectionManager connectionManager;
2626

2727

28-
/**
29-
* create a client pool for http client .
30-
*/
31-
/**
32-
* create a client pool for http client .
33-
*/
34-
/**
35-
* create a client pool for http client .
36-
*/
3728
public static HttpClientPool getHttpClient() {
3829
HttpClientPool tmp = clientInstance;
3930
if (tmp == null) {
@@ -48,12 +39,6 @@ public static HttpClientPool getHttpClient() {
4839
return tmp;
4940
}
5041

51-
/**
52-
* creates a new httpclient object .
53-
*/
54-
/**
55-
* creates a new httpclient object .
56-
*/
5742
private HttpClientPool() {
5843
connectionManager = new PoolingHttpClientConnectionManager();
5944
connectionManager.setMaxTotal(200);// 连接池

0 commit comments

Comments
 (0)