From 7cdf25cc3b4996f77f32929f31ae6d8589c29510 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 27 Oct 2024 12:09:13 +0100 Subject: [PATCH] fix(extension): Add languages natively to VS Code --- vs-code-extension/package.json | 62 +++++++++++++++++-- .../src/events/on-undetectable.ts | 34 ++++++++++ vs-code-extension/src/events/types.ts | 6 ++ vs-code-extension/src/extension.ts | 62 ++++++++----------- 4 files changed, 122 insertions(+), 42 deletions(-) create mode 100644 vs-code-extension/src/events/on-undetectable.ts create mode 100644 vs-code-extension/src/events/types.ts diff --git a/vs-code-extension/package.json b/vs-code-extension/package.json index 5705ae6..609b7f6 100644 --- a/vs-code-extension/package.json +++ b/vs-code-extension/package.json @@ -13,6 +13,62 @@ "Formatters" ], "preview": true, + "contributes": { + "languages": [ + { + "id": "sshconfig", + "extensions": ["sshconfig", "ssh_config"], + "aliases": ["SSH Config", "sshconfig"], + "filenames": ["sshconfig", "ssh_config"], + "filenamePatterns": ["~/.ssh/config", "**/sshconfig", "**/ssh_config"] + }, + { + "id": "sshdconfig", + "extensions": ["sshdconfig", "sshd_config"], + "aliases": ["SSH Daemon Config", "sshdconfig"], + "filenames": ["sshdconfig", "sshd_config"], + "filenamePatterns": ["/etc/ssh/sshd_config", "**/sshdconfig", "**/sshd_config"] + }, + { + "id": "aliases", + "extensions": ["aliases", "mailaliases"], + "aliases": ["Mail Aliases", "aliases", "mailaliases"], + "filenames": ["aliases", "mailaliases"], + "filenamePatterns": ["/etc/aliases", "**/aliases", "**/mailaliases"] + }, + { + "id": "fstab", + "extensions": ["fstab"], + "aliases": ["fstab"], + "filenames": ["fstab"], + "filenamePatterns": ["/etc/fstab", "**/fstab", "**/etc/fstab"] + }, + { + "id": "hosts", + "extensions": ["hosts"], + "aliases": ["hosts"], + "filenames": ["hosts"], + "filenamePatterns": ["/etc/hosts", "**/hosts", "**/etc/hosts"] + }, + { + "id": "wireguard", + "extensions": ["wireguard", "wg"], + "aliases": ["WireGuard", "wireguard", "wg"], + "filenames": ["wireguard", "wg0.conf", "wg1.conf", "wg0", "wg1"], + "filenamePatterns": ["/etc/wireguard/*.conf", "**/wireguard", "**/wireguard.conf"] + } + ] + }, + "activationEvents": [ + "onLanguage:plaintext", + "onLanguage:yaml", + "onLanguage:sshconfig", + "onLanguage:sshdconfig", + "onLanguage:aliases", + "onLanguage:fstab", + "onLanguage:hosts", + "onLanguage:wireguard" + ], "sponsor": { "url": "https://github.com/Myzel394/contact-me" }, @@ -50,16 +106,12 @@ "engines": { "vscode": "^1.74.0" }, - "activationEvents": [ - "onLanguage:plaintext", - "onLanguage:yaml" - ], "main": "./out/extension.js", "scripts": { "compile": "node esbuild.js", "compile:prod": "node esbuild.js --production", "watch": "tsc -b -w", - "lint": "eslint ./src --ext .ts,.tsx" + "lint": "eslint ./src" }, "dependencies": { "vscode-languageclient": "^9.0.1", diff --git a/vs-code-extension/src/events/on-undetectable.ts b/vs-code-extension/src/events/on-undetectable.ts new file mode 100644 index 0000000..0802071 --- /dev/null +++ b/vs-code-extension/src/events/on-undetectable.ts @@ -0,0 +1,34 @@ +import { GenericNotificationHandler } from "vscode-languageclient"; +import * as vscode from "vscode"; + +const ACTION_SELECT_LANGUAGE = "Select Language"; +const ACTION_DISABLE = "Ignore for this file"; + +const ignoredFiled = new Set(); + +export const onUndetectable: GenericNotificationHandler = async (params: LSPLanguageUndetectable) => { + if (ignoredFiled.has(params.Uri)) { + return; + } + + const result = await vscode.window.showWarningMessage( + "config-lsp was unable to detect the appropriate language for this file", + { + detail: "Either select a language or add '#?lsp.language=' to the top of the file", + }, + ACTION_SELECT_LANGUAGE, + ACTION_DISABLE, + ) + + switch (result) { + case ACTION_SELECT_LANGUAGE: + vscode.commands.executeCommand("workbench.action.editor.changeLanguageMode"); + break; + case ACTION_DISABLE: + ignoredFiled.add(params.Uri); + break; + undefined: + break; + } +} + diff --git a/vs-code-extension/src/events/types.ts b/vs-code-extension/src/events/types.ts new file mode 100644 index 0000000..8bae362 --- /dev/null +++ b/vs-code-extension/src/events/types.ts @@ -0,0 +1,6 @@ +interface LSPNotification { + Uri: string; +} + +interface LSPLanguageUndetectable extends LSPNotification {} + diff --git a/vs-code-extension/src/extension.ts b/vs-code-extension/src/extension.ts index 661ed43..c07b319 100644 --- a/vs-code-extension/src/extension.ts +++ b/vs-code-extension/src/extension.ts @@ -1,32 +1,30 @@ -import * as path from "path" -import { ExtensionContext, workspace } from 'vscode'; +import * as path from "path"; +import { ExtensionContext, workspace } from "vscode"; import { Executable, LanguageClient, type LanguageClientOptions, type ServerOptions, -} from 'vscode-languageclient/node'; +} from "vscode-languageclient/node"; +import { onUndetectable } from "./events/on-undetectable"; -const IS_DEBUG = process.env.VSCODE_DEBUG_MODE === 'true' || process.env.NODE_ENV === 'development'; +const IS_DEBUG = + process.env.VSCODE_DEBUG_MODE === "true" || + process.env.NODE_ENV === "development"; let client: LanguageClient; -export function activate(context: ExtensionContext) { +export async function activate({subscriptions}: ExtensionContext) { console.info("config-lsp activated"); - const initOptions = workspace.getConfiguration('config-lsp'); + const initOptions = workspace.getConfiguration("config-lsp"); const clientOptions: LanguageClientOptions = { documentSelector: [ - { - scheme: 'file', - language: 'plaintext', - pattern: "**/{config,sshconfig,sshd_config,sshdconfig,fstab,hosts,aliases}", - }, - // Some configs seem to be incorrectly detected as yaml - { - scheme: 'file', - language: 'yaml', - pattern: "**/{config,sshconfig,sshd_config,sshdconfig,fstab,hosts,aliases}", - }, + {language: "sshconfig"}, + {language: "sshdconfig"}, + {language: "aliases"}, + {language: "fstab"}, + {language: "hosts"}, + {language: "wireguard"}, ], initializationOptions: initOptions, }; @@ -34,39 +32,29 @@ export function activate(context: ExtensionContext) { const path = getBundledPath(); console.info(`Found config-lsp path at ${path}`); const run: Executable = { - command: getBundledPath(), - } + command: getBundledPath() , + }; const serverOptions: ServerOptions = { run, debug: run, - } + }; client = new LanguageClient( - 'config-lsp', + "config-lsp", serverOptions, clientOptions, - IS_DEBUG, + IS_DEBUG ); + console.info("Starting config-lsp..."); + await client.start(); + console.info("Started config-lsp"); - client.start(); - console.info("config-lsp started"); - - // const serverOptions: ServerOptions = { - // } - // - // // Create the language client and start the client. - // client = new LanguageClient( - // 'languageServerExample', - // clientOptions - // ); - // - // // Start the client. This will also launch the server - // client.start(); + subscriptions.push(client.onNotification("$/config-lsp/languageUndetectable", onUndetectable)) } function getBundledPath(): string { - const filePath = path.resolve(__dirname, "config-lsp") + const filePath = path.resolve(__dirname, "config-lsp"); return filePath; }