Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vscode hbs plugin, additional features #238

Merged
merged 7 commits into from
Nov 26, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { commands, window, Location, Uri, Range, Position } from "vscode";
import { type Location as LSLocation } from "vscode-languageclient/node";

/**
* converts locations of type vscode-languageclient/node to `Location` from `vscode` (client) extension
*
* @param {LSLocation} locations - code locations
* @returns {Location}
*/
function castLocationsType({ uri, range }: LSLocation) {
return new Location(
Uri.file(uri),
new Range(new Position(range.start.line, range.start.character), new Position(range.end.line, range.end.character)),
);
}

// trigger vscode build-in command to navigate to the given code location(s)
export function goToLocation(location: LSLocation) {
commands.executeCommand(
"editor.action.goToLocations",
window.activeTextEditor.document.uri, // from
window.activeTextEditor.selection.active,
[castLocationsType(location)], // to
"peek", // for multiple options (if we use LSLocation[]), show the list and allow the user to choose
"",
);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as path from "path";
import type { ExtensionContext } from "vscode";
import { type ExtensionContext, commands } from "vscode";
import { LanguageClient, TransportKind } from "vscode-languageclient/node";
import type { LanguageClientOptions, ServerOptions } from "vscode-languageclient/node";
import { goToLocation } from "./commands";

let client: LanguageClient;

Expand All @@ -25,8 +26,11 @@ export function activate(context: ExtensionContext): void {

// Options to control the language client
const clientOptions: LanguageClientOptions = {
// Register the server for Handlebars documents
documentSelector: [{ scheme: "file", language: "handlebars" }],
// Register the server for Handlebars documents and Typescript
documentSelector: [
{ scheme: "file", language: "handlebars" },
{ scheme: "file", language: "typescript" },
],
};

// Create the language client and start the client.
Expand All @@ -39,6 +43,10 @@ export function activate(context: ExtensionContext): void {

// Start the client. This will also launch the server
client.start();

// running "editor.action.goToLocations" command directly in server code didn't work,
// custom command to get the arguments and execute command from the client code
commands.registerCommand("P!VHandlebarsLanguageServer.codelensAction", goToLocation);
}

export function deactivate(): Thenable<void> | undefined {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2019",
"lib": ["ES2019"],
"target": "ESNext",
"lib": ["ESNext"],
"module": "commonjs",
"moduleResolution": "node",
"outDir": "out",
Expand Down
Loading
Loading