Skip to content

Commit dc9a1a9

Browse files
Merge pull request #733 from testforstephen/jinbo_triggerfiles
Take the opened java files as triggerFiles and add to the LS initialization parameters
2 parents 68d4bed + 4a2e449 commit dc9a1a9

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

src/extension.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
7070
extendedClientCapabilities:{
7171
progressReportProvider: getJavaConfiguration().get('progressReports.enabled'),
7272
classFileContentsSupport:true
73-
}
73+
},
74+
triggerFiles: getTriggerFiles()
7475
},
7576
revealOutputChannelOn: RevealOutputChannelOn.Never
7677
};
@@ -690,3 +691,55 @@ async function executeRangeFormat(editor, startPosition, lineOffset) {
690691
editor.selection = new Selection(startPosition, endPosition);
691692
await commands.executeCommand('editor.action.formatSelection');
692693
}
694+
695+
function getTriggerFiles(): string[] {
696+
const openedJavaFiles = [];
697+
const activeJavaFile = getJavaFilePathOfTextEditor(window.activeTextEditor);
698+
if (activeJavaFile) {
699+
openedJavaFiles.push(Uri.file(activeJavaFile).toString());
700+
}
701+
702+
if (!workspace.workspaceFolders) {
703+
return openedJavaFiles;
704+
}
705+
706+
for (const rootFolder of workspace.workspaceFolders) {
707+
if (rootFolder.uri.scheme !== "file") {
708+
continue;
709+
}
710+
711+
const rootPath = path.normalize(rootFolder.uri.fsPath);
712+
if (isPrefix(rootPath, activeJavaFile)) {
713+
continue;
714+
}
715+
716+
for (const textEditor of window.visibleTextEditors) {
717+
const javaFileInTextEditor = getJavaFilePathOfTextEditor(textEditor);
718+
if (isPrefix(rootPath, javaFileInTextEditor)) {
719+
openedJavaFiles.push(Uri.file(javaFileInTextEditor).toString());
720+
break;
721+
}
722+
}
723+
}
724+
725+
return openedJavaFiles;
726+
}
727+
728+
function getJavaFilePathOfTextEditor(editor: TextEditor): string | undefined {
729+
if (editor) {
730+
const resource = editor.document.uri;
731+
if (resource.scheme === "file" && resource.fsPath.endsWith(".java")) {
732+
return path.normalize(resource.fsPath);
733+
}
734+
}
735+
736+
return undefined;
737+
}
738+
739+
function isPrefix(parentPath: string, childPath: string): boolean {
740+
if (!childPath) {
741+
return false;
742+
}
743+
const relative = path.relative(parentPath, childPath);
744+
return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative);
745+
}

0 commit comments

Comments
 (0)