Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as path from 'path';
import * as os from 'os';
import * as fs from 'fs';
import { workspace, extensions, ExtensionContext, window, StatusBarAlignment, commands, ViewColumn, Uri, CancellationToken, TextDocumentContentProvider, TextEditor, WorkspaceConfiguration, languages, IndentAction, ProgressLocation, InputBoxOptions, Selection, Position, EventEmitter, OutputChannel } from 'vscode';
import { workspace, extensions, ExtensionContext, window, StatusBarAlignment, commands, ViewColumn, Uri, CancellationToken, TextDocumentContentProvider, WorkspaceConfiguration, languages, IndentAction, ProgressLocation, InputBoxOptions, Selection, Position, EventEmitter, OutputChannel, TextDocument } from 'vscode';
import { ExecuteCommandParams, ExecuteCommandRequest, LanguageClient, LanguageClientOptions, RevealOutputChannelOn, Position as LSPosition, Location as LSLocation, StreamInfo, VersionedTextDocumentIdentifier, ErrorHandler, Message, ErrorAction, CloseAction, InitializationFailedHandler, DidChangeConfigurationNotification } from 'vscode-languageclient';
import { onExtensionChange, collectJavaExtensions } from './plugin';
import { prepareExecutable, awaitServerConnection } from './javaServerStarter';
Expand Down Expand Up @@ -135,8 +135,9 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
});
// rethrow to disrupt the chain.
throw error;
}).then(requirements => {
return new Promise((resolve, reject) => {
}).then(async (requirements) => {
const triggerFiles = await getTriggerFiles();
return new Promise<ExtensionAPI>((resolve, reject) => {
const workspacePath = path.resolve(storagePath + '/jdt_ws');
// Options to control the language client
const clientOptions: LanguageClientOptions = {
Expand Down Expand Up @@ -167,7 +168,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
moveRefactoringSupport: true,
clientHoverProvider: true,
},
triggerFiles: getTriggerFiles()
triggerFiles,
},
middleware: {
workspace: {
Expand Down Expand Up @@ -822,9 +823,9 @@ async function executeRangeFormat(editor, startPosition, lineOffset) {
await commands.executeCommand('editor.action.formatSelection');
}

function getTriggerFiles(): string[] {
async function getTriggerFiles(): Promise<string[]> {
const openedJavaFiles = [];
const activeJavaFile = getJavaFilePathOfTextEditor(window.activeTextEditor);
const activeJavaFile = getJavaFilePathOfTextDocument(window.activeTextEditor && window.activeTextEditor.document);
if (activeJavaFile) {
openedJavaFiles.push(Uri.file(activeJavaFile).toString());
}
Expand All @@ -833,31 +834,53 @@ function getTriggerFiles(): string[] {
return openedJavaFiles;
}

for (const rootFolder of workspace.workspaceFolders) {
await Promise.all(workspace.workspaceFolders.map(async (rootFolder) => {
if (rootFolder.uri.scheme !== 'file') {
continue;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? if subsequent folders have the file:// prefix, why should they be discarded?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm so here return simply returns from the lambda, so it's fine

@Vigilans Vigilans Dec 18, 2019 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I change the for...of pattern to forEach, and here return is equivalent to continue, just to continue the outer loop...

return;
}

const rootPath = path.normalize(rootFolder.uri.fsPath);
if (isPrefix(rootPath, activeJavaFile)) {
continue;
return;
}

for (const textEditor of window.visibleTextEditors) {
const javaFileInTextEditor = getJavaFilePathOfTextEditor(textEditor);
const javaFileInTextEditor = getJavaFilePathOfTextDocument(textEditor.document);
if (isPrefix(rootPath, javaFileInTextEditor)) {
openedJavaFiles.push(Uri.file(javaFileInTextEditor).toString());
break;
return;
}
}
}

for (const textDocument of workspace.textDocuments) {
const javaFileInTextDocument = getJavaFilePathOfTextDocument(textDocument);
if (isPrefix(rootPath, javaFileInTextDocument)) {
openedJavaFiles.push(Uri.file(javaFileInTextDocument).toString());
return;
}
}

for (const javaFile of await workspace.findFiles("*.java", undefined, 1)) { // Find at most 1 java file
if (isPrefix(rootPath, javaFile.fsPath)) {
openedJavaFiles.push(javaFile.toString());
return;
}
}

for (const javaFile of await workspace.findFiles("{src, test}/**/*.java", undefined, 1)) {
if (isPrefix(rootPath, javaFile.fsPath)) {
openedJavaFiles.push(javaFile.toString());
return;
}
}
}));

return openedJavaFiles;
}

function getJavaFilePathOfTextEditor(editor: TextEditor): string | undefined {
if (editor) {
const resource = editor.document.uri;
function getJavaFilePathOfTextDocument(document: TextDocument): string | undefined {
if (document) {
const resource = document.uri;
if (resource.scheme === 'file' && resource.fsPath.endsWith('.java')) {
return path.normalize(resource.fsPath);
}
Expand Down