Skip to content

Commit 76cbc17

Browse files
author
jordanbreen28
committed
(CAT-1669) - Fix hashrocket allignment on completion item select
Prior to this commit, when selecting a param/prop from the dropdown completion list in puppet-vscode, the hash rocket would not be formatted inline with the rest of the document. This commit implements a middleware to the language client, and adds a vscode API item command which allows us to execute a vscode command after the selection of the param/prop from the dropdown list. So when an param/prop is selected, we send a callback event to the language server to format the document. This is only possible on the vscode side thanks to the vscode API, unfortunately there is no suitable events to implement this into the langauge server.
1 parent 2681e30 commit 76cbc17

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/extension.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ export function activate(context: vscode.ExtensionContext) {
133133
extensionFeatures.push(new PuppetModuleHoverFeature(extContext, logger));
134134
}
135135

136+
// if formatting is enabled, add a middleware to the language client to format the document on insert
137+
if (settings.format.enable) {
138+
vscode.commands.registerCommand('editor.action.formatDocumentAndMoveCursor', async () => {
139+
vscode.commands.executeCommand('editor.action.formatDocument').then(() => {
140+
vscode.commands.executeCommand('cursorMove', { to: 'left' });
141+
});
142+
});
143+
// add middleware to Intercept the provideCompletionItem method
144+
connectionHandler.languageClient.clientOptions.middleware = provideCompletionItemMiddleware;
145+
}
146+
136147
const facts = new PuppetFactsProvider(connectionHandler);
137148
vscode.window.registerTreeDataProvider('puppetFacts', facts);
138149

@@ -359,3 +370,28 @@ function setLanguageConfiguration() {
359370
},
360371
});
361372
}
373+
374+
export const provideCompletionItemMiddleware = {
375+
provideCompletionItem: async (document, position, context, token, next) => {
376+
// Get the completion list from the language server
377+
const result = await next(document, position, context, token);
378+
let items: vscode.CompletionItem[];
379+
if (Array.isArray(result)) {
380+
items = result;
381+
} else {
382+
items = result.items;
383+
}
384+
// Add command to be executed after completion item is selected
385+
items.forEach(item => {
386+
// check completion item is a prop or param, as this dictates which command to use
387+
const isPropOrParam = item.detail === 'Property' || item.detail === 'Parameter';
388+
// additional cursor cmd on the insertion of a property/param (UX)
389+
const command = isPropOrParam ? 'editor.action.formatDocumentAndMoveCursor' : 'editor.action.formatDocument';
390+
item.command = {
391+
title: 'format document',
392+
command: command
393+
};
394+
});
395+
return items;
396+
}
397+
};

0 commit comments

Comments
 (0)