From fc5558da0a06e231628e81c7a9a8126f412c026e Mon Sep 17 00:00:00 2001 From: ShortArrow Date: Thu, 4 Jan 2024 00:15:54 +0900 Subject: [PATCH 1/5] Update relative line numbers in core.ts --- src/core.ts | 67 ++++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/src/core.ts b/src/core.ts index 217de34..a95f810 100644 --- a/src/core.ts +++ b/src/core.ts @@ -43,43 +43,48 @@ export async function updateRelativeLineNumbers( const repeatingDigitsColor = getColorAtRepeatingDigits(); const centerColorOfRainbow = getColorAtCenterOfRainbow(); const labelWidth = document.lineCount.toString().length; + const endLine = document.lineCount < editor.visibleRanges[0].end.line + 2 ? document.lineCount : editor.visibleRanges[0].end.line + 2; + const startLine = editor.visibleRanges[0].start.line - 1 < 0 ? 0 : editor.visibleRanges[0].start.line; + for (let lineIndex = startLine; lineIndex < endLine; lineIndex++) { + try { + const lineRange = document.lineAt(lineIndex).range; + const isCurrentLine = lineIndex === activeLineNumber; - for (let lineIndex = editor.visibleRanges[0].start.line ; lineIndex < editor.visibleRanges[0].end.line + 2; lineIndex++) { - const lineRange = document.lineAt(lineIndex).range; - const isCurrentLine = lineIndex === activeLineNumber; + const label = isCurrentLine + ? String(activeLineNumber + 1) + : String(Math.abs(lineIndex - activeLineNumber)); - const label = isCurrentLine - ? String(activeLineNumber + 1) - : String(Math.abs(lineIndex - activeLineNumber)); - - const rangeScope = new vscode.Range(lineRange.start, lineRange.start); - const lineNumberStyle = { - width: `${labelWidth / 2 + 0.5}em`, - align: "right", - contentText: label, - color: isCurrentLine - ? activeLineNumberColor - : (enableRepeatingDigits && isRepeatingDigits(label)) - ? repeatingDigitsColor - : enableRainbow - ? shiftHue(centerColorOfRainbow, Math.abs(lineIndex - activeLineNumber)) - : inactiveLineNumberColor, - textDecoration: ` + const rangeScope = new vscode.Range(lineRange.start, lineRange.start); + const lineNumberStyle = { + width: `${labelWidth / 2 + 0.5}em`, + align: "right", + contentText: label, + color: isCurrentLine + ? activeLineNumberColor + : (enableRepeatingDigits && isRepeatingDigits(label)) + ? repeatingDigitsColor + : enableRainbow + ? shiftHue(centerColorOfRainbow, Math.abs(lineIndex - activeLineNumber)) + : inactiveLineNumberColor, + textDecoration: ` box-sizing: border-box; text-align: right; padding-right: 1em; `, - fontWeight: "bold", - } as vscode.DecorationInstanceRenderOptions; - const lineNumberAreaStyle: vscode.DecorationInstanceRenderOptions = { - before: lineNumberStyle, - } as vscode.DecorationInstanceRenderOptions; - const decoration: vscode.DecorationOptions = { - range: rangeScope, - renderOptions: lineNumberAreaStyle, - }; - - decorations.push(decoration); + fontWeight: "bold", + } as vscode.DecorationInstanceRenderOptions; + const lineNumberAreaStyle: vscode.DecorationInstanceRenderOptions = { + before: lineNumberStyle, + } as vscode.DecorationInstanceRenderOptions; + const decoration: vscode.DecorationOptions = { + range: rangeScope, + renderOptions: lineNumberAreaStyle, + }; + decorations.push(decoration); + } + catch (error) { + console.error(error); + } } const enableRelativeLine = getEnableRelativeLine(); From 30fb7aefddc74cacd0d4aa7304fb02a0b57b3196 Mon Sep 17 00:00:00 2001 From: ShortArrow Date: Thu, 4 Jan 2024 00:17:43 +0900 Subject: [PATCH 2/5] Update package.json version to 0.0.8 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0ab32cf..6ca0819 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "LineNumberDeco", "description": "VSCode extension to display relative line numbers", "publisher": "ShortArrow", - "version": "0.0.7", + "version": "0.0.8", "engines": { "vscode": "^1.76.0" }, From 05ea5d48f69d347c0f099dabff687120dc26071d Mon Sep 17 00:00:00 2001 From: ShortArrow Date: Thu, 4 Jan 2024 00:19:31 +0900 Subject: [PATCH 3/5] Fix decoration update bugs --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91a52d8..fb3e822 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -47,3 +47,8 @@ All notable changes to the "ShortArrow.line-number-deco" extension will be docum - Add 'nvim' and 'helix' to `README.md` - Decorated only visible parts to improve speed - Delete Commands list and Configuration list from `README.md` + +## 0.0.8 + +- Fix bug that decoration is not updated when the cursor is moved to the end of the line +- Fix bug that decoration is not updated when the document size is small than the editor size From 41702446c6562ba297e24cb66739af92d35e099a Mon Sep 17 00:00:00 2001 From: ShortArrow Date: Sat, 24 Feb 2024 18:52:14 +0900 Subject: [PATCH 4/5] Update package.json and extension.ts --- BUILD.md | 34 +++++++ generater/fromPackageJson.ts | 76 ++++++++++++++ package.json | 5 +- src/extension.ts | 55 +++++------ src/generated/generated.ts | 186 +++++++++++++++++++++++++++++++++++ 5 files changed, 322 insertions(+), 34 deletions(-) create mode 100644 BUILD.md create mode 100644 generater/fromPackageJson.ts create mode 100644 src/generated/generated.ts diff --git a/BUILD.md b/BUILD.md new file mode 100644 index 0000000..d056593 --- /dev/null +++ b/BUILD.md @@ -0,0 +1,34 @@ +# How to build the project + +## Prerequisites + +- [Visual Studio Code](https://code.visualstudio.com/) +- [Node.js](https://nodejs.org/) +- [Yarn V4](https://yarnpkg.com/cli) + +## Build + +Generate `src/generated` directory. + +```bash +mkdir -p ./src/generated +yarn dlx generater/fromPackageJson.ts +``` + +or this command. + +```bash +yarn generate +``` + +Compile the TypeScript code. + +```bash +yarn compile +``` + +## Deploy + +```bash +yarn package +``` diff --git a/generater/fromPackageJson.ts b/generater/fromPackageJson.ts new file mode 100644 index 0000000..fd6aa76 --- /dev/null +++ b/generater/fromPackageJson.ts @@ -0,0 +1,76 @@ +import { readFile } from 'fs/promises'; +import { writeFile } from 'fs/promises'; + +function getFilePath() { + return 'package.json'; +} + +async function getKeyValue(key: string): Promise { + try { + // read the file asynchronously + const fileContent = await readFile(getFilePath(), 'utf8'); + // parse the read JSON into an object + const json = JSON.parse(fileContent); + // return the value of the specified key + return json[key] || null; + } catch (error) { + console.error('Error reading or parsing JSON file:', error); + return null; + } +} + +async function readCommands(): Promise { + try { + // read the file asynchronously + const fileContent = await readFile(getFilePath(), 'utf8'); + // parse the read JSON into an object + const json = JSON.parse(fileContent); + // return the 'contributes.commands' array + return json.contributes?.commands || null; + } catch (error) { + console.error('Error reading or parsing JSON file:', error); + return null; + } +} + +async function getExtensionName() { + const name = await getKeyValue('name'); + return name || 'No name found'; +} + +async function generateFile(outputPath: string) { + const commands = await readCommands(); + if (!commands) { + throw new Error('No extension commands found'); + } + const thisExtension = (await getExtensionName()) + .replace(/-(.)/g, (_match: string, group1: string) => group1.toUpperCase()) + .replace(/^(.)/, (match: string) => match.toUpperCase()); + const warning = `// This file is generated from package.json.\n// Do not modify this file manually.\n\n`; + const importVscode = `import * as vscode from "vscode";\n\n`; + const classHeader = `export class ${thisExtension} {\n`; + const classFooter = `\n}`; + const lines = commands.map((command: any) => { + const { command: name, title } = command; + const commandName = name.split('.').pop(); + return ( + ` /** + * ${title} + */ + static ${commandName}(callback: Function) { + return vscode.commands.registerCommand(\'${name}\', () => + callback() + ); + }` + ); + }); + const content = + warning + + importVscode + + classHeader + + lines.join('\n\n') + + classFooter; + await writeFile(outputPath, content); +} + +generateFile('./src/generated/generated.ts'); \ No newline at end of file diff --git a/package.json b/package.json index 6ca0819..1e2e4a8 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "LineNumberDeco", "description": "VSCode extension to display relative line numbers", "publisher": "ShortArrow", - "version": "0.0.8", + "version": "0.0.9", "engines": { "vscode": "^1.76.0" }, @@ -181,7 +181,8 @@ "watch": "tsc -watch -p ./", "pretest": "yarn run clean && yarn run compile && yarn run lint", "lint": "eslint src --ext ts", - "test": "yarn run pretest && node ./out/test/runTest.js" + "test": "yarn run pretest && node ./out/test/runTest.js", + "generate": "yarn dlx ts-node ./generater/fromPackageJson.ts" }, "devDependencies": { "@types/glob": "^8.1.0", diff --git a/src/extension.ts b/src/extension.ts index 2b0c084..08d964d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,5 +1,5 @@ import * as vscode from "vscode"; -import { +import { getColorCodeAtActiveRowNumber, getColorCodeAtActiveRowNumberForUser, getColorCodeAtCenterOfRainbow, @@ -16,15 +16,10 @@ import { updateEnableRepeatingDigitsForUser, } from "./ui"; import { updateRelativeLineNumbers } from "./core"; +import { LineNumberDeco } from "./generated/generated"; const decorationType = vscode.window.createTextEditorDecorationType({}); -function cmdAssign(command: string, callback: Function) { - return vscode.commands.registerCommand(`line-number-deco.${command}`, () => - callback() - ); -} - const commands = [ vscode.workspace.onDidChangeTextDocument((e) => { if (e.document === vscode.window.activeTextEditor?.document) { @@ -42,30 +37,26 @@ const commands = [ updateRelativeLineNumbers(vscode.window.activeTextEditor, decorationType); } }), - ...[ - {name: "enableRelativeLineNumbers", func: updateEnableRelativeLine, value: true}, - {name: "disableRelativeLineNumbers", func: updateEnableRelativeLine, value: false}, - {name: "enableRelativeLineNumbersForUser", func: updateEnableRelativeLineForUser, value: true}, - {name: "disableRelativeLineNumbersForUser", func: updateEnableRelativeLineForUser, value: false}, - {name: "enableRainbow", func: updateEnableRainbowForWorkspace, value: true}, - {name: "disableRainbow", func: updateEnableRainbowForWorkspace, value: false}, - {name: "enableRainbowForUser", func: updateEnableRainbowForUser, value: true}, - {name: "disableRainbowForUser", func: updateEnableRainbowForUser, value: false}, - {name: "enableRepeatingDigits", func: updateEnableRepeatingDigits, value: true}, - {name: "disableRepeatingDigits", func: updateEnableRepeatingDigits, value: false}, - {name: "enableRepeatingDigitsForUser", func: updateEnableRepeatingDigitsForUser, value: true}, - {name: "disableRepeatingDigitsForUser", func: updateEnableRepeatingDigitsForUser, value: false}, - {name: "updateColorAtRepeatingDigits", func: getColorCodeAtRepeatingDigits, value: undefined}, - {name: "updateColorAtRepeatingDigitsForUser", func: getColorCodeAtRepeatingDigitsForUser, value: undefined}, - {name: "updateColorAtCenterOfRainbow", func: getColorCodeAtCenterOfRainbow, value: undefined}, - {name: "updateColorAtCenterOfRainbowForUser", func: getColorCodeAtCenterOfRainbowForUser, value: undefined}, - {name: "updateColorAtInactiveRowNumberForUser", func: getColorCodeAtInactiveRowNumberForUser, value: undefined}, - {name: "updateColorAtActiveRowNumberForUser", func: getColorCodeAtActiveRowNumberForUser, value: undefined}, - {name: "updateColorAtAInactiveRowNumber", func: getColorCodeAtInactiveRowNumber, value: undefined}, - {name: "updateColorAtActiveRowNumber", func: getColorCodeAtActiveRowNumber, value: undefined}, - ].map(({name, func, value}) => { - return cmdAssign(name, () => value === undefined ? func() : func(value)); - }), + LineNumberDeco.enableRelativeLineNumbers(() => updateEnableRelativeLine(true)), + LineNumberDeco.disableRelativeLineNumbers(() => updateEnableRelativeLine(false)), + LineNumberDeco.enableRelativeLineNumbersForUser(() => updateEnableRelativeLineForUser(true)), + LineNumberDeco.disableRelativeLineNumbersForUser(() => updateEnableRelativeLineForUser(false)), + LineNumberDeco.enableRainbow(() => updateEnableRainbowForWorkspace(true)), + LineNumberDeco.disableRainbow(() => updateEnableRainbowForWorkspace(false)), + LineNumberDeco.enableRainbowForUser(() => updateEnableRainbowForUser(true)), + LineNumberDeco.disableRainbowForUser(() => updateEnableRainbowForUser(false)), + LineNumberDeco.enableRepeatingDigits(() => updateEnableRepeatingDigits(true)), + LineNumberDeco.disableRepeatingDigits(() => updateEnableRepeatingDigits(false)), + LineNumberDeco.enableRepeatingDigitsForUser(() => updateEnableRepeatingDigitsForUser(true)), + LineNumberDeco.disableRepeatingDigitsForUser(() => updateEnableRepeatingDigitsForUser(false)), + LineNumberDeco.updateColorAtRepeatingDigits(getColorCodeAtRepeatingDigits), + LineNumberDeco.updateColorAtRepeatingDigitsForUser(getColorCodeAtRepeatingDigitsForUser), + LineNumberDeco.updateColorAtCenterOfRainbow(getColorCodeAtCenterOfRainbow), + LineNumberDeco.updateColorAtCenterOfRainbowForUser(getColorCodeAtCenterOfRainbowForUser), + LineNumberDeco.updateColorAtInactiveRowNumberForUser(getColorCodeAtInactiveRowNumberForUser), + LineNumberDeco.updateColorAtActiveRowNumberForUser(getColorCodeAtActiveRowNumberForUser), + LineNumberDeco.updateColorAtInactiveRowNumber(getColorCodeAtInactiveRowNumber), + LineNumberDeco.updateColorAtActiveRowNumber(getColorCodeAtActiveRowNumber), ]; /** @@ -79,4 +70,4 @@ export function activate(context: vscode.ExtensionContext) { /** * deactivate extension */ -export function deactivate() {} +export function deactivate() { } diff --git a/src/generated/generated.ts b/src/generated/generated.ts new file mode 100644 index 0000000..a2746a4 --- /dev/null +++ b/src/generated/generated.ts @@ -0,0 +1,186 @@ +// This file is generated from package.json. +// Do not modify this file manually. + +import * as vscode from "vscode"; + +export class LineNumberDeco { + /** + * LineNumberDeco: Enable rainbow for workspace + */ + static enableRainbow(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.enableRainbow', () => + callback() + ); + } + + /** + * LineNumberDeco: Disable rainbow for workspace + */ + static disableRainbow(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.disableRainbow', () => + callback() + ); + } + + /** + * LineNumberDeco: Enable rainbow for user + */ + static enableRainbowForUser(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.enableRainbowForUser', () => + callback() + ); + } + + /** + * LineNumberDeco: Disable rainbow for user + */ + static disableRainbowForUser(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.disableRainbowForUser', () => + callback() + ); + } + + /** + * LineNumberDeco: Update color at center of rainbow for workspace + */ + static updateColorAtCenterOfRainbow(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.updateColorAtCenterOfRainbow', () => + callback() + ); + } + + /** + * LineNumberDeco: Update color at center of rainbow for user + */ + static updateColorAtCenterOfRainbowForUser(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.updateColorAtCenterOfRainbowForUser', () => + callback() + ); + } + + /** + * LineNumberDeco: Update color at current row number + */ + static updateColorAtActiveRowNumber(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.updateColorAtActiveRowNumber', () => + callback() + ); + } + + /** + * LineNumberDeco: Update color at current row number for user + */ + static updateColorAtActiveRowNumberForUser(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.updateColorAtActiveRowNumberForUser', () => + callback() + ); + } + + /** + * LineNumberDeco: Update color at inactive row number + */ + static updateColorAtInactiveRowNumber(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.updateColorAtInactiveRowNumber', () => + callback() + ); + } + + /** + * LineNumberDeco: Update color at inactive row number for user + */ + static updateColorAtInactiveRowNumberForUser(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.updateColorAtInactiveRowNumberForUser', () => + callback() + ); + } + + /** + * LineNumberDeco: Update color of repeating digits for workspace + */ + static updateColorAtRepeatingDigits(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.updateColorAtRepeatingDigits', () => + callback() + ); + } + + /** + * LineNumberDeco: Update color of repeating digits for user + */ + static updateColorAtRepeatingDigitsForUser(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.updateColorAtRepeatingDigitsForUser', () => + callback() + ); + } + + /** + * LineNumberDeco: Enable Relative Line Numbers in This workspace + */ + static enableRelativeLineNumbers(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.enableRelativeLineNumbers', () => + callback() + ); + } + + /** + * LineNumberDeco: Enable Relative Line Numbers for user + */ + static enableRelativeLineNumbersForUser(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.enableRelativeLineNumbersForUser', () => + callback() + ); + } + + /** + * LineNumberDeco: Disable Relative Line Numbers in This workspace + */ + static disableRelativeLineNumbers(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.disableRelativeLineNumbers', () => + callback() + ); + } + + /** + * LineNumberDeco: Disable Relative Line Numbers for user + */ + static disableRelativeLineNumbersForUser(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.disableRelativeLineNumbersForUser', () => + callback() + ); + } + + /** + * LineNumberDeco: Enable repeating digits in this workspace + */ + static enableRepeatingDigits(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.enableRepeatingDigits', () => + callback() + ); + } + + /** + * LineNumberDeco: Disable repeating digits color in this workspace + */ + static disableRepeatingDigits(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.disableRepeatingDigits', () => + callback() + ); + } + + /** + * LineNumberDeco: Enable repeating digits color for user + */ + static enableRepeatingDigitsForUser(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.enableRepeatingDigitsForUser', () => + callback() + ); + } + + /** + * LineNumberDeco: Disable repeating digits color for user + */ + static disableRepeatingDigitsForUser(callback: Function) { + return vscode.commands.registerCommand('line-number-deco.disableRepeatingDigitsForUser', () => + callback() + ); + } +} \ No newline at end of file From dfff310e9619e36c851dea570aebd2d3811bf2ab Mon Sep 17 00:00:00 2001 From: ShortArrow Date: Sun, 25 Feb 2024 15:49:38 +0900 Subject: [PATCH 5/5] Add extensionKind property to package.json --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index 1e2e4a8..429f487 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,10 @@ "categories": [ "Other" ], + "extensionKind": [ + "ui", + "workspace" + ], "activationEvents": [ "onStartupFinished" ],