From e67a362789a6ba3d0495a3726e06a36683a42653 Mon Sep 17 00:00:00 2001 From: "Wu, Zhenyu" Date: Sat, 4 Jan 2025 05:36:47 +0800 Subject: [PATCH] Support (Neo)Vim by coc.nvim Refer: https://github.com/valentjn/vscode-ltex/blob/3d0cb8cd9b4d0dc8ef6c08a4f376767820678cf6/src/extension.ts#L8-L14 Difference is vscode-ltex/coc-ltex uses a customized python script https://github.com/valentjn/vscode-ltex/blob/3d0cb8cd9b4d0dc8ef6c08a4f376767820678cf6/tools/patchForTarget.py to patch code we use c language macro preprocessor cpp --- coc-variables/.gitignore | 3 ++ coc-variables/.npmignore | 4 +++ coc-variables/README.md | 20 +++++++++++++ coc-variables/package.json | 33 ++++++++++++++++++++ coc-variables/scripts/patch.pl | 2 ++ coc-variables/scripts/patch.sh | 7 +++++ coc-variables/src/.gitignore | 2 ++ coc-variables/tsconfig.json | 10 +++++++ src/index.ts | 55 ++++++++++++++++++++++++++++++---- 9 files changed, 130 insertions(+), 6 deletions(-) create mode 100644 coc-variables/.gitignore create mode 100644 coc-variables/.npmignore create mode 100644 coc-variables/README.md create mode 100644 coc-variables/package.json create mode 100755 coc-variables/scripts/patch.pl create mode 100755 coc-variables/scripts/patch.sh create mode 100644 coc-variables/src/.gitignore create mode 100644 coc-variables/tsconfig.json diff --git a/coc-variables/.gitignore b/coc-variables/.gitignore new file mode 100644 index 0000000..f7fdfcf --- /dev/null +++ b/coc-variables/.gitignore @@ -0,0 +1,3 @@ +dist/ +node_modules +.DS_Store diff --git a/coc-variables/.npmignore b/coc-variables/.npmignore new file mode 100644 index 0000000..de2bf00 --- /dev/null +++ b/coc-variables/.npmignore @@ -0,0 +1,4 @@ +/* +!/out/ +*.map +!/prebuilds/ diff --git a/coc-variables/README.md b/coc-variables/README.md new file mode 100644 index 0000000..306954c --- /dev/null +++ b/coc-variables/README.md @@ -0,0 +1,20 @@ +# Coc.nvim Predefined Variable Parser + +Ported from [vscode-variables](https://github.com/DominicVonk/vscode-variables). + +## Install + +- [coc-marketplace](https://github.com/fannheyward/coc-marketplace) +- [npm](https://www.npmjs.com/package/coc-variables) +- vim: + +```vim +" command line +CocInstall coc-variables +" or add the following code to your vimrc +let g:coc_global_extensions = ['coc-variables', 'other coc-plugins'] +``` + +## Usage + +Refer [vscode-variables](https://github.com/DominicVonk/vscode-variables). diff --git a/coc-variables/package.json b/coc-variables/package.json new file mode 100644 index 0000000..ac28867 --- /dev/null +++ b/coc-variables/package.json @@ -0,0 +1,33 @@ +{ + "name": "coc-variables", + "version": "0.0.1", + "description": "Replace Coc.nvim predefined variables for extensions", + "scripts": { + "patch": "scripts/patch.sh ../src/*.ts", + "prepack": "npm run patch && npm run build", + "build": "tsc", + "test": "vitest run" + }, + "files": [ + "package.json", + "README.md", + "LICENSE.md", + "dist" + ], + "main": "dist/index.js", + "devDependencies": { + "@types/node": "^22.9.0", + "coc.nvim": "^0.0.83-next.18", + "typescript": "^5.6.3", + "vitest": "^2.1.4" + }, + "author": "Dominic Vonk", + "license": "MIT", + "repository": { + "url": "https://github.com/DominicVonk/vscode-variables.git", + "type": "git" + }, + "bugs": { + "url": "https://github.com/DominicVonk/vscode-variables/issues" + } +} diff --git a/coc-variables/scripts/patch.pl b/coc-variables/scripts/patch.pl new file mode 100755 index 0000000..5363953 --- /dev/null +++ b/coc-variables/scripts/patch.pl @@ -0,0 +1,2 @@ +#!/usr/bin/env -S perl -p +s=//#==; diff --git a/coc-variables/scripts/patch.sh b/coc-variables/scripts/patch.sh new file mode 100755 index 0000000..00894aa --- /dev/null +++ b/coc-variables/scripts/patch.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -e +cd "$(dirname "$(dirname "$(readlink -f "$0")")")" + +for file; do + scripts/patch.pl "$file" | cpp -DHAVE_COC_NVIM -nostdinc -P -C -o"${file#*/}" +done diff --git a/coc-variables/src/.gitignore b/coc-variables/src/.gitignore new file mode 100644 index 0000000..a68d087 --- /dev/null +++ b/coc-variables/src/.gitignore @@ -0,0 +1,2 @@ +/* +!/.gitignore diff --git a/coc-variables/tsconfig.json b/coc-variables/tsconfig.json new file mode 100644 index 0000000..7b52bd4 --- /dev/null +++ b/coc-variables/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../tsconfig", + "compilerOptions": { + "outDir": "dist", + "noImplicitAny": false + }, + "include": [ + "src/**/*" + ] +} diff --git a/src/index.ts b/src/index.ts index f5ecbbc..88fd1dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,28 +1,59 @@ import * as os from "os"; import * as process from "process"; +//# #if HAVE_VSCODE import * as vscode from "vscode"; +//# #elif HAVE_COC_NVIM +//# import * as vscode from "coc.nvim"; +//# #endif module.exports = async function variables(string: string, recursive = false) { const workspaceFolders = vscode.workspace.workspaceFolders; const activeFile = vscode.window.activeTextEditor?.document; + //# #if HAVE_VSCODE const absoluteFilePath = activeFile?.uri.fsPath; + //# #elif HAVE_COC_NVIM + //# const absoluteFilePath = activeFile?.uri + //# ? vscode.Uri.parse(activeFile.uri).fsPath + //# : null; + //# #endif const workspace = vscode.workspace.workspaceFolders?.[0]; + //# #if HAVE_VSCODE const activeWorkspace = workspaceFolders?.find((workspace) => absoluteFilePath?.startsWith(workspace.uri.fsPath) )?.uri.fsPath; + //# #elif HAVE_COC_NVIM + //# const uri = workspaceFolders?.find((workspace) => + //# absoluteFilePath?.startsWith(vscode.Uri.parse(workspace.uri).fsPath) + //# )?.uri; + //# const activeWorkspace = uri ? vscode.Uri.parse(uri).fsPath : null; + //# #endif const homeDir = os.homedir(); // ${userHome} - /home/your-username string = string.replace(/\${userHome}/g, homeDir); // ${workspaceFolder} - /home/your-username/your-project - string = string.replace(/\${workspaceFolder}/g, workspace?.uri.fsPath ?? ""); + string = string.replace( + /\${workspaceFolder}/g, + //# #if HAVE_VSCODE + workspace?.uri.fsPath + //# #elif HAVE_COC_NVIM + //# vscode.Uri.parse(workspace?.uri).fsPath ?? "" + //# #endif + ); // ${workspaceFolder:name} - /home/your-username/your-project2 string = string.replace(/\${workspaceFolder:(.*?)}/g, function (_, name) { + //# #if HAVE_VSCODE return ( workspaceFolders?.find((workspace) => workspace.name === name)?.uri .fsPath ?? "" ); + //# #elif HAVE_COC_NVIM + //# const uri = workspaceFolders?.find( + //# (workspace) => workspace.name === name + //# )?.uri; + //# return uri ? vscode.Uri.parse(uri).fsPath : ""; + //# #endif }); // ${workspaceFolderBasename} - your-project @@ -90,7 +121,11 @@ module.exports = async function variables(string: string, recursive = false) { string = string.replace( /\${lineNumber}/g, (vscode.window.activeTextEditor + //# #if HAVE_VSCODE ? vscode.window.activeTextEditor.selection.start.line + 1 + //# #elif HAVE_COC_NVIM + //# ? vscode.window.activeTextEditor.visibleRanges[0].start.line + 1 + //# #endif : 0 ).toString() ); @@ -98,12 +133,20 @@ module.exports = async function variables(string: string, recursive = false) { // ${selectedText} - text selected in your code editor string = string.replace(/\${selectedText}/g, function () { return ( - vscode.window.activeTextEditor?.document.getText( - new vscode.Range( - vscode.window.activeTextEditor.selection.start, - vscode.window.activeTextEditor.selection.end + vscode.window.activeTextEditor?.document + //# #if HAVE_VSCODE + .getText( + new vscode.Range( + vscode.window.activeTextEditor.selection.start, + vscode.window.activeTextEditor.selection.end + ) ) - ) ?? "" + //# #elif HAVE_COC_NVIM + //# .getLines( + //# vscode.window.activeTextEditor.visibleRanges[0].start.line, + //# vscode.window.activeTextEditor.visibleRanges[0].end.line + //# ).join("\n") ?? "" + //# #endif ); });