Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions coc-variables/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist/
node_modules
.DS_Store
4 changes: 4 additions & 0 deletions coc-variables/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*
!/out/
*.map
!/prebuilds/
20 changes: 20 additions & 0 deletions coc-variables/README.md
Original file line number Diff line number Diff line change
@@ -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).
33 changes: 33 additions & 0 deletions coc-variables/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
2 changes: 2 additions & 0 deletions coc-variables/scripts/patch.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env -S perl -p
s=//#==;
7 changes: 7 additions & 0 deletions coc-variables/scripts/patch.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions coc-variables/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*
!/.gitignore
10 changes: 10 additions & 0 deletions coc-variables/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../tsconfig",
"compilerOptions": {
"outDir": "dist",
"noImplicitAny": false
},
"include": [
"src/**/*"
]
}
55 changes: 49 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -90,20 +121,32 @@ 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()
);

// ${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
);
});

Expand Down