Skip to content
Merged
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ export function activate(context: vscode.ExtensionContext) {
extensionFeatures.push(new PuppetModuleHoverFeature(extContext, logger));
}

// if formatting is enabled, add a middleware to the language client to format the document on insert
if (settings.format.enable) {
vscode.commands.registerCommand('editor.action.formatDocumentAndMoveCursor', async () => {
vscode.commands.executeCommand('editor.action.formatDocument').then(() => {
vscode.commands.executeCommand('cursorMove', { to: 'left' });
});
});
// add middleware to Intercept the provideCompletionItem method
connectionHandler.languageClient.clientOptions.middleware = provideCompletionItemMiddleware;
}

const facts = new PuppetFactsProvider(connectionHandler);
vscode.window.registerTreeDataProvider('puppetFacts', facts);

Expand Down Expand Up @@ -359,3 +370,28 @@ function setLanguageConfiguration() {
},
});
}

export const provideCompletionItemMiddleware = {
provideCompletionItem: async (document, position, context, token, next) => {
// Get the completion list from the language server
const result = await next(document, position, context, token);
let items: vscode.CompletionItem[];
if (Array.isArray(result)) {
items = result;
} else {
items = result.items;
}
// Add command to be executed after completion item is selected
items.forEach(item => {
// check completion item is a prop or param, as this dictates which command to use
const isPropOrParam = item.detail === 'Property' || item.detail === 'Parameter';
// additional cursor cmd on the insertion of a property/param (UX)
const command = isPropOrParam ? 'editor.action.formatDocumentAndMoveCursor' : 'editor.action.formatDocument';
item.command = {
title: 'format document',
command: command
};
});
return items;
}
};
66 changes: 66 additions & 0 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as assert from 'assert';
import { after, before, beforeEach, describe, it } from 'mocha';
import * as sinon from 'sinon';
import * as vscode from 'vscode';
import { provideCompletionItemMiddleware } from '../../extension';
import { PuppetStatusBarFeature } from '../../feature/PuppetStatusBarFeature';
import { ISettings, settingsFromWorkspace } from '../../settings';

describe('Extension Tests', () => {
let vscodeCommandsRegisterCommandStub: sinon.SinonStub;
let puppetStatusBarFeatureStub: sinon.SinonStub;
let settings: ISettings;
let context: vscode.ExtensionContext;
let registerDebugAdapterDescriptorFactoryStub: sinon.SinonStub;
let document: vscode.TextDocument;
let position: vscode.Position;
let completionContext: vscode.CompletionContext;
let token: vscode.CancellationToken;
let next: sinon.SinonStub
const sandbox = sinon.createSandbox();

before(() => {
vscodeCommandsRegisterCommandStub = sandbox.stub(vscode.commands, 'registerCommand');
settings = settingsFromWorkspace();
context = {
subscriptions: [],
asAbsolutePath: (relativePath: string) => {
return `/absolute/path/to/${relativePath}`;
},
globalState: {
get: sandbox.stub(),
}
} as vscode.ExtensionContext;
puppetStatusBarFeatureStub = sandbox.createStubInstance(PuppetStatusBarFeature);
registerDebugAdapterDescriptorFactoryStub = sandbox.stub(vscode.debug, 'registerDebugAdapterDescriptorFactory');
});

beforeEach(() => {
document = {} as vscode.TextDocument;
position = new vscode.Position(0, 0);
completionContext = {} as vscode.CompletionContext;
token = new vscode.CancellationTokenSource().token;
next = sandbox.stub();
});

after(() => {
sandbox.restore();
});

it('should add command to completion items', async () => {
const completionItems = [
new vscode.CompletionItem('item1', vscode.CompletionItemKind.Property),
new vscode.CompletionItem('item2', vscode.CompletionItemKind.Text),
];
completionItems[0].detail = 'Property';
completionItems[1].detail = 'Text';
next.returns(completionItems);

const result = await provideCompletionItemMiddleware.provideCompletionItem(document, position, context, token, next);

assert.ok(Array.isArray(result));
assert.strictEqual(result.length, 2);
assert.strictEqual(result[0].command?.command, 'editor.action.formatDocumentAndMoveCursor');
assert.strictEqual(result[1].command?.command, 'editor.action.formatDocument');
});
});