How filter out completion tips that have syntax errors
I'm developing a sentence level java completion plugin. The completion tips are generated by AI models. But some of them contain syntax errors and I want to filter out these tips.
I've tried some ideas.
Firstly, I concatenate a tip and raw source code, then get a new PSIFile and its VirtualFile. And I use CodeSmellDetector to get error infos. But I always get empty results list. This method just works well for a real VirtualFile(which really exists on disk)but not a LightVirtualFile(which is created from a PSIFile only-in-memory).
String srcNew = beforeContext + tip + afterContext;
PsiFile newPsiFile = PsiFileFactory.getInstance(project).createFileFromText(psiFile.getName(),
psiFile.getFileType(), srcNew);
VirtualFile virtualFile = psiFile.getViewProvider().getVirtualFile();
List<CodeSmellInfo> codeSmells = CodeSmellDetector.getInstance(project).findCodeSmells(Collections.singletonList(virtualFile));
Then, I wirte srcNew to a temp file and get a VirtualFile, then I delete the temp file.By this method, I can get right results when I type code character one by one. But when I type code fast, I get unexpected results. I think it is caused by multi-thread proplems, but I have no idea about how to solve.
srcNew = srcNew.replace(ori, des);
try {
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(desPath));
writer.write(srcNew.getBytes());
writer.close();
} catch (IOException e) {
}
VirtualFile newFile = LocalFileSystem.getInstance().findFileByIoFile(new File(desPath));
List<CodeSmellInfo> newCodeSmells = CodeSmellDetector.getInstance(project).findCodeSmells(Collections.singletonList(newFile));
try {
File file = new File(desPath);
file.delete();
} catch (Exception e) {
}
I prefer the first idea, but I don't know how to check systax errors for a PSIFile only-in-memory. Your suggestions will be greatly appreciated.
Please sign in to leave a comment.
I'm not sure how detecting code smells in a "tip" (short code snippet) is supposed to give valid results, given there's little or no context in which such inspections could work. Could you please clarify what kind of content you show for completion and what your exact expectations about checking validity of such snippets are?