From 4504d04d953f72a29a4a8a40c70cf0d04aaa136c Mon Sep 17 00:00:00 2001 From: garjones Date: Mon, 8 Oct 2018 19:09:13 -0300 Subject: [PATCH] set document langauge if it is a manifest file --- package.json | 2 +- src/extension.ts | 16 ++++++++++++++++ src/wrappers.ts | 4 ++++ test/extension.test.ts | 16 ++++++++++++++++ test/mocks.ts | 8 ++++++++ 5 files changed, 45 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index d9a875b..92e2128 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "url": "https://github.com/g-arjones/vscode-autoproj.git" }, "engines": { - "vscode": "^1.27.0" + "vscode": "^1.28.0" }, "activationEvents": [ "*" diff --git a/src/extension.ts b/src/extension.ts index d02ea6e..fefe017 100644 --- a/src/extension.ts +++ b/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"; @@ -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 { try { await ws.reload(); @@ -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) => { diff --git a/src/wrappers.ts b/src/wrappers.ts index 4ab255a..22b8dcb 100644 --- a/src/wrappers.ts +++ b/src/wrappers.ts @@ -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 { + return vscode.languages.setTextDocumentLanguage(document, languageId); + } } diff --git a/test/extension.test.ts b/test/extension.test.ts index ebd2d15..1cdff6c 100644 --- a/test/extension.test.ts +++ b/test/extension.test.ts @@ -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()", () => { diff --git a/test/mocks.ts b/test/mocks.ts index 0174a1b..edc6a6f 100644 --- a/test/mocks.ts +++ b/test/mocks.ts @@ -40,6 +40,14 @@ export function createTaskProcessEndEvent(definition: vscode.TaskDefinition, exi return mockTaskProcessEndEvent.object; } +export function createOpenTextDocumentEvent(docPath: string) { + const mockOpenTextDocumentEvent = Mock.ofType(); + const uri = vscode.Uri.file(docPath); + + mockOpenTextDocumentEvent.setup((x) => x.uri).returns(() => uri); + return mockOpenTextDocumentEvent.object; +} + export class MockWorkspaces { public readonly mock: IMock;