Skip to content

Commit

Permalink
Merge pull request #27 from g-arjones/change_document_language
Browse files Browse the repository at this point in the history
set document langauge if it is a manifest file
  • Loading branch information
g-arjones committed Oct 8, 2018
2 parents c7509ba + 4504d04 commit c1363a1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -28,7 +28,7 @@
"url": "https://github.com/g-arjones/vscode-autoproj.git"
},
"engines": {
"vscode": "^1.27.0"
"vscode": "^1.28.0"
},
"activationEvents": [
"*"
Expand Down
16 changes: 16 additions & 0 deletions src/extension.ts
@@ -1,6 +1,7 @@
"use strict";
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as path from "path";
import * as vscode from "vscode";
import * as autoproj from "./autoproj";
import * as commands from "./commands";
Expand Down Expand Up @@ -41,6 +42,18 @@ export class EventHandler implements vscode.Disposable {
}
}

public onDidOpenTextDocument(event: vscode.TextDocument) {
const docName = path.basename(event.uri.fsPath);
const docDir = path.dirname(event.uri.fsPath);

for (const [, ws] of this._workspaces.workspaces) {
if ((docDir === path.join(ws.root, "autoproj")) && (docName.startsWith("manifest."))) {
this._wrapper.setTextDocumentLanguage(event, "yaml");
break;
}
}
}

public async onManifestChanged(ws: autoproj.Workspace): Promise<void> {
try {
await ws.reload();
Expand Down Expand Up @@ -129,6 +142,9 @@ export function setupExtension(subscriptions: any[], vscodeWrapper: wrappers.VSC
eventHandler.onDidStartTaskProcess(event);
tasksHandler.onDidStartTaskProcess(event);
}));
subscriptions.push(vscode.workspace.onDidOpenTextDocument((event: vscode.TextDocument) => {
eventHandler.onDidOpenTextDocument(event);
}));
subscriptions.push(vscode.tasks.onDidEndTaskProcess((event) => tasksHandler.onDidEndTaskProcess(event)));
subscriptions.push(vscode.workspace.onDidChangeConfiguration((event) => autoprojTaskProvider.reloadTasks()));
subscriptions.push(vscode.workspace.onDidChangeWorkspaceFolders((event) => {
Expand Down
4 changes: 4 additions & 0 deletions src/wrappers.ts
Expand Up @@ -65,4 +65,8 @@ export class VSCode {
public getConfiguration(section?: string, resource?: vscode.Uri | null): vscode.WorkspaceConfiguration {
return vscode.workspace.getConfiguration(section, resource);
}

public setTextDocumentLanguage(document: vscode.TextDocument, languageId: string): Thenable<vscode.TextDocument> {
return vscode.languages.setTextDocumentLanguage(document, languageId);
}
}
16 changes: 16 additions & 0 deletions test/extension.test.ts
Expand Up @@ -277,6 +277,22 @@ describe("EventHandler", () => {
mockWrapper.verify((x) => x.showErrorMessage(It.isAny()), Times.once());
});
});
describe("onDidOpenTextDocument", () => {
beforeEach(() => {
mockWorkspaces.addWorkspace("/a/foo/workspace");
mockWorkspaces.addWorkspace("/path/to/workspace");
});
it("changes the document language if file name starts with 'manifest.'", () => {
const event = mocks.createOpenTextDocumentEvent("/path/to/workspace/autoproj/manifest.robot");
subject.onDidOpenTextDocument(event);
mockWrapper.verify((x) => x.setTextDocumentLanguage(event, "yaml"), Times.once());
});
it("keeps the document language", () => {
const event = mocks.createOpenTextDocumentEvent("/path/to/workspace/autoproj/init.rb");
subject.onDidOpenTextDocument(event);
mockWrapper.verify((x) => x.setTextDocumentLanguage(It.isAny(), It.isAny()), Times.never());
});
});
});

describe("extension.setupExtension()", () => {
Expand Down
8 changes: 8 additions & 0 deletions test/mocks.ts
Expand Up @@ -40,6 +40,14 @@ export function createTaskProcessEndEvent(definition: vscode.TaskDefinition, exi
return mockTaskProcessEndEvent.object;
}

export function createOpenTextDocumentEvent(docPath: string) {
const mockOpenTextDocumentEvent = Mock.ofType<vscode.TextDocument>();
const uri = vscode.Uri.file(docPath);

mockOpenTextDocumentEvent.setup((x) => x.uri).returns(() => uri);
return mockOpenTextDocumentEvent.object;
}

export class MockWorkspaces {
public readonly mock: IMock<autoproj.Workspaces>;

Expand Down

0 comments on commit c1363a1

Please sign in to comment.