fix(extension): Add languages natively to VS Code

This commit is contained in:
Myzel394 2024-10-27 12:09:13 +01:00
parent f56951cb8d
commit 7cdf25cc3b
No known key found for this signature in database
GPG Key ID: ED20A1D1D423AF3F
4 changed files with 122 additions and 42 deletions

View File

@ -13,6 +13,62 @@
"Formatters" "Formatters"
], ],
"preview": true, "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": { "sponsor": {
"url": "https://github.com/Myzel394/contact-me" "url": "https://github.com/Myzel394/contact-me"
}, },
@ -50,16 +106,12 @@
"engines": { "engines": {
"vscode": "^1.74.0" "vscode": "^1.74.0"
}, },
"activationEvents": [
"onLanguage:plaintext",
"onLanguage:yaml"
],
"main": "./out/extension.js", "main": "./out/extension.js",
"scripts": { "scripts": {
"compile": "node esbuild.js", "compile": "node esbuild.js",
"compile:prod": "node esbuild.js --production", "compile:prod": "node esbuild.js --production",
"watch": "tsc -b -w", "watch": "tsc -b -w",
"lint": "eslint ./src --ext .ts,.tsx" "lint": "eslint ./src"
}, },
"dependencies": { "dependencies": {
"vscode-languageclient": "^9.0.1", "vscode-languageclient": "^9.0.1",

View File

@ -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<string>();
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=<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;
}
}

View File

@ -0,0 +1,6 @@
interface LSPNotification {
Uri: string;
}
interface LSPLanguageUndetectable extends LSPNotification {}

View File

@ -1,32 +1,30 @@
import * as path from "path" import * as path from "path";
import { ExtensionContext, workspace } from 'vscode'; import { ExtensionContext, workspace } from "vscode";
import { import {
Executable, Executable,
LanguageClient, LanguageClient,
type LanguageClientOptions, type LanguageClientOptions,
type ServerOptions, 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; let client: LanguageClient;
export function activate(context: ExtensionContext) { export async function activate({subscriptions}: ExtensionContext) {
console.info("config-lsp activated"); console.info("config-lsp activated");
const initOptions = workspace.getConfiguration('config-lsp'); const initOptions = workspace.getConfiguration("config-lsp");
const clientOptions: LanguageClientOptions = { const clientOptions: LanguageClientOptions = {
documentSelector: [ documentSelector: [
{ {language: "sshconfig"},
scheme: 'file', {language: "sshdconfig"},
language: 'plaintext', {language: "aliases"},
pattern: "**/{config,sshconfig,sshd_config,sshdconfig,fstab,hosts,aliases}", {language: "fstab"},
}, {language: "hosts"},
// Some configs seem to be incorrectly detected as yaml {language: "wireguard"},
{
scheme: 'file',
language: 'yaml',
pattern: "**/{config,sshconfig,sshd_config,sshdconfig,fstab,hosts,aliases}",
},
], ],
initializationOptions: initOptions, initializationOptions: initOptions,
}; };
@ -34,39 +32,29 @@ export function activate(context: ExtensionContext) {
const path = getBundledPath(); const path = getBundledPath();
console.info(`Found config-lsp path at ${path}`); console.info(`Found config-lsp path at ${path}`);
const run: Executable = { const run: Executable = {
command: getBundledPath(), command: getBundledPath() ,
} };
const serverOptions: ServerOptions = { const serverOptions: ServerOptions = {
run, run,
debug: run, debug: run,
} };
client = new LanguageClient( client = new LanguageClient(
'config-lsp', "config-lsp",
serverOptions, serverOptions,
clientOptions, clientOptions,
IS_DEBUG, IS_DEBUG
); );
console.info("Starting config-lsp...");
await client.start();
console.info("Started config-lsp");
client.start(); subscriptions.push(client.onNotification("$/config-lsp/languageUndetectable", onUndetectable))
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();
} }
function getBundledPath(): string { function getBundledPath(): string {
const filePath = path.resolve(__dirname, "config-lsp") const filePath = path.resolve(__dirname, "config-lsp");
return filePath; return filePath;
} }