Skip to content

Commit

Permalink
Added workspaceContains language server activation
Browse files Browse the repository at this point in the history
Signed-off-by: jpinkney <josh.pinkney@mail.utoronto.ca>
  • Loading branch information
JPinkney authored and kittaakos committed Jul 6, 2018
1 parent 6546be8 commit f517558
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/java/src/browser/java-client-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class JavaClientContribution extends BaseLanguageClientContribution {
return ['**/*.java', '**/pom.xml', '**/*.gradle'];
}

protected get workspaceContains() {
return ['pom.xml', 'build.gradle'];
}

protected onReady(languageClient: ILanguageClient): void {
languageClient.onNotification(ActionableNotification.type, this.showActionableMessage.bind(this));
super.onReady(languageClient);
Expand Down
2 changes: 2 additions & 0 deletions packages/languages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"@theia/core": "^0.3.12",
"@theia/output": "^0.3.12",
"@theia/process": "^0.3.12",
"@theia/workspace": "^0.3.12",
"vscode-base-languageclient": "^0.0.1-alpha.5",
"vscode-languageserver-protocol": "^3.6.0"
},
Expand Down Expand Up @@ -49,3 +50,4 @@
"extends": "../../configs/nyc.json"
}
}

34 changes: 33 additions & 1 deletion packages/languages/src/browser/language-client-contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
Workspace, Languages, Commands
} from '../common';
import { LanguageClientFactory } from "./language-client-factory";
import { WorkspaceService } from "@theia/workspace/lib/browser";

export const LanguageClientContribution = Symbol('LanguageClientContribution');
export interface LanguageClientContribution extends LanguageContribution {
Expand All @@ -45,6 +46,7 @@ export abstract class BaseLanguageClientContribution implements LanguageClientCo

@inject(MessageService) protected readonly messageService: MessageService;
@inject(CommandRegistry) protected readonly registry: CommandRegistry;
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;

constructor(
@inject(Workspace) protected readonly workspace: Workspace,
Expand All @@ -59,11 +61,26 @@ export abstract class BaseLanguageClientContribution implements LanguageClientCo
}

waitForActivation(app: FrontendApplication): Promise<any> {
const activationPromises: Promise<any>[] = [];
const workspaceContains = this.workspaceContains;
if (workspaceContains.length !== 0) {
activationPromises.push(this.waitForItemInWorkspace());
}
const documentSelector = this.documentSelector;
if (documentSelector) {
activationPromises.push(this.waitForOpenTextDocument(documentSelector));
}
if (activationPromises.length !== 0) {
return Promise.all([
this.workspace.ready,
this.waitForOpenTextDocument(documentSelector)
Promise.race(activationPromises.map(p => new Promise(async resolve => {
try {
await p;
resolve();
} catch (e) {
console.error(e);
}
})))
]);
}
return this.workspace.ready;
Expand Down Expand Up @@ -116,6 +133,10 @@ export abstract class BaseLanguageClientContribution implements LanguageClientCo
};
}

protected get workspaceContains(): string[] {
return [];
}

protected get documentSelector(): DocumentSelector | undefined {
return [this.id];
}
Expand All @@ -134,6 +155,17 @@ export abstract class BaseLanguageClientContribution implements LanguageClientCo
return [];
}

/**
* Check to see if one of the paths is in the current workspace.
*/
protected async waitForItemInWorkspace(): Promise<any> {
const doesContain = await this.workspaceService.containsSome(this.workspaceContains);
if (!doesContain) {
return new Promise(resolve => { });
}
return doesContain;
}

// FIXME move it to the workspace
protected waitForOpenTextDocument(selector: DocumentSelector): Promise<TextDocument> {
const document = this.workspace.textDocuments.filter(doc =>
Expand Down
19 changes: 19 additions & 0 deletions packages/workspace/src/browser/workspace-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ export class WorkspaceService implements FrontendApplicationContribution {
return options !== undefined && !!options.preserveWindow;
}

/**
* Return true if one of the paths in paths array is present in the workspace
* NOTE: You should always explicitly use `/` as the separator between the path segments.
*/
async containsSome(paths: string[]): Promise<boolean> {
const workspaceRoot = await this.root;
if (workspaceRoot) {
const uri = new URI(workspaceRoot.uri);
for (const path of paths) {
const fileUri = uri.resolve(path).toString();
const exists = await this.fileSystem.exists(fileUri);
if (exists) {
return exists;
}
}
}
return false;
}

}

export interface WorkspaceInput {
Expand Down

0 comments on commit f517558

Please sign in to comment.