forked from openpilot-hub/devpilot-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve completion result, handle some special case of specific AI mo…
…del (openpilot-hub#44)
- Loading branch information
1 parent
69b2078
commit 234f0bd
Showing
4 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/main/java/com/zhongan/devpilot/treesitter/TreeSitterParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package com.zhongan.devpilot.treesitter; | ||
|
||
import com.zhongan.devpilot.util.LanguageUtil; | ||
|
||
import java.util.Locale; | ||
|
||
import org.treesitter.TSLanguage; | ||
import org.treesitter.TSParser; | ||
import org.treesitter.TSTree; | ||
import org.treesitter.TreeSitterGo; | ||
import org.treesitter.TreeSitterJava; | ||
import org.treesitter.TreeSitterPython; | ||
|
||
public class TreeSitterParser { | ||
private final TSLanguage language; | ||
|
||
public TreeSitterParser(TSLanguage language) { | ||
this.language = language; | ||
} | ||
|
||
public String clearRedundantWhitespace(String originCode, int position, String output) { | ||
if (language == null) { | ||
return output; | ||
} | ||
|
||
var result = new StringBuilder(output); | ||
while (result.length() != 0 && result.charAt(0) == ' ') { | ||
result.deleteCharAt(0); | ||
if (containsError(buildFullCode(originCode, position, result.toString()))) { | ||
return " " + result; | ||
} | ||
} | ||
|
||
return result.toString(); | ||
} | ||
|
||
public String parse(String originCode, int position, String output) { | ||
if (!output.startsWith(" ")) { | ||
return parseInner(originCode, position, output); | ||
} | ||
|
||
// handle special case : start with several whitespace | ||
var noWhitespaceResult = parseInner(originCode, position, output.trim()); | ||
var whitespaceResult = parseInner(originCode, position, " " + output.trim()); | ||
|
||
var result = whitespaceResult.length() < noWhitespaceResult.length() | ||
? noWhitespaceResult : whitespaceResult; | ||
|
||
return clearRedundantWhitespace(originCode, position, result); | ||
} | ||
|
||
private String parseInner(String originCode, int position, String output) { | ||
if (language == null) { | ||
return output; | ||
} | ||
|
||
var result = new StringBuilder(output); | ||
while (result.length() != 0) { | ||
if (containsError(buildFullCode(originCode, position, result.toString()))) { | ||
result.deleteCharAt(result.length() - 1); | ||
} else { | ||
return result.toString(); | ||
} | ||
} | ||
|
||
return output; | ||
} | ||
|
||
private String buildFullCode(String originCode, int position, String output) { | ||
StringBuilder stringBuilder = new StringBuilder(originCode); | ||
stringBuilder.insert(position, output); | ||
return stringBuilder.toString(); | ||
} | ||
|
||
private boolean containsError(String input) { | ||
var treeString = getTree(input).getRootNode().toString(); | ||
return treeString.contains("ERROR") | ||
|| treeString.contains("MISSING \"}\"") | ||
|| treeString.contains("MISSING \")\""); | ||
} | ||
|
||
private TSTree getTree(String input) { | ||
var parser = new TSParser(); | ||
parser.setLanguage(language); | ||
return parser.parseString(null, input); | ||
} | ||
|
||
public static TreeSitterParser getInstance(String extension) { | ||
var language = LanguageUtil.getLanguageByExtension(extension); | ||
|
||
if (language == null) { | ||
return new TreeSitterParser(null); | ||
} | ||
|
||
TSLanguage tsLanguage = null; | ||
|
||
switch (language.getLanguageName().toLowerCase(Locale.ROOT)) { | ||
case "java": | ||
tsLanguage = new TreeSitterJava(); | ||
break; | ||
case "go": | ||
tsLanguage = new TreeSitterGo(); | ||
break; | ||
case "python": | ||
tsLanguage = new TreeSitterPython(); | ||
break; | ||
} | ||
|
||
return new TreeSitterParser(tsLanguage); | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/test/java/com/zhongan/devpilot/treesitter/TreeSitterParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.zhongan.devpilot.treesitter; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class TreeSitterParserTest { | ||
@Test | ||
public void testJavaParse() { | ||
var wholeFile = "package org.example;\n" + | ||
"\n" + | ||
"public class AgentTest {\n" + | ||
" public static void main(String[] args) {\n" + | ||
" System.out.println(\"\");\n" + | ||
" }\n" + | ||
"}\n" + | ||
"\n" + | ||
"\n"; | ||
var position = 120; | ||
var insertCode = "AgentTest completed.\");"; | ||
|
||
var parser = TreeSitterParser.getInstance("java"); | ||
var result = parser.parse(wholeFile, position, insertCode); | ||
|
||
Assert.assertEquals(result, "AgentTest completed."); | ||
|
||
wholeFile = "package org.example;\n" + | ||
"\n" + | ||
"public class AgentTest {\n" + | ||
" public static void main(String[] args) {\n" + | ||
" System.out.println(\"\");\n" + | ||
" }\n" + | ||
"}\n" + | ||
"\n" + | ||
"\n"; | ||
insertCode = " AgentTest completed.\");"; | ||
|
||
parser = TreeSitterParser.getInstance("java"); | ||
result = parser.parse(wholeFile, position, insertCode); | ||
|
||
Assert.assertEquals(result, "AgentTest completed."); | ||
|
||
wholeFile = "package org.example;\n" + | ||
"\n" + | ||
"public class AgentTest {\n" + | ||
" public static void main(String[] args) {\n" + | ||
" String" + | ||
" System.out.println(\"AgentTest completed\");\n" + | ||
" }\n" + | ||
"}\n" + | ||
"\n" + | ||
"\n"; | ||
position = 106; | ||
insertCode = " str = \"AgentTest\";"; | ||
|
||
parser = TreeSitterParser.getInstance("java"); | ||
result = parser.parse(wholeFile, position, insertCode); | ||
|
||
Assert.assertEquals(result, "str = \"AgentTest\";"); | ||
|
||
wholeFile = "package org.example;\n" + | ||
"\n" + | ||
"public class AgentTest {\n" + | ||
" public static void main(String[] args) {\n" + | ||
" Sys" + | ||
" System.out.println(\"AgentTest completed2\");\n" + | ||
" }\n" + | ||
"}\n" + | ||
"\n" + | ||
"\n"; | ||
position = 103; | ||
insertCode = " tem.out.println(\"AgentTest completed1\");"; | ||
|
||
parser = TreeSitterParser.getInstance("java"); | ||
result = parser.parse(wholeFile, position, insertCode); | ||
|
||
Assert.assertEquals(result, "tem.out.println(\"AgentTest completed1\");"); | ||
} | ||
|
||
@Test | ||
public void testPythonParse() { | ||
var wholeFile = "import pandas as pd\n" + | ||
"\n" + | ||
"if __name__ == '__main__':\n" + | ||
" df = pd.read_excel(r'test.xlsx')\n" + | ||
" for idx, row in tqdm(df.iterrows(), t ):\n" + | ||
"\n" + | ||
"\n" + | ||
" print('done')"; | ||
var position = 126; | ||
var insertCode = "otal=df.shape[0]):"; | ||
|
||
var parser = TreeSitterParser.getInstance("py"); | ||
var result = parser.parse(wholeFile, position, insertCode); | ||
|
||
Assert.assertEquals(result, "otal=df.shape[0]"); | ||
} | ||
} |