Skip to content

Commit

Permalink
Merge pull request #28 from ShortArrow/develop
Browse files Browse the repository at this point in the history
Release v0.0.9
  • Loading branch information
ShortArrow authored Feb 25, 2024
2 parents 0c7d71d + dfff310 commit 2fc551f
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 65 deletions.
34 changes: 34 additions & 0 deletions BUILD.md
Original file line number Diff line number Diff line change
@@ -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
```
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
76 changes: 76 additions & 0 deletions generater/fromPackageJson.ts
Original file line number Diff line number Diff line change
@@ -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<string | null> {
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<any[] | null> {
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');
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "LineNumberDeco",
"description": "VSCode extension to display relative line numbers",
"publisher": "ShortArrow",
"version": "0.0.7",
"version": "0.0.9",
"engines": {
"vscode": "^1.76.0"
},
Expand All @@ -22,6 +22,10 @@
"categories": [
"Other"
],
"extensionKind": [
"ui",
"workspace"
],
"activationEvents": [
"onStartupFinished"
],
Expand Down Expand Up @@ -181,7 +185,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",
Expand Down
67 changes: 36 additions & 31 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
55 changes: 23 additions & 32 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from "vscode";
import {
import {
getColorCodeAtActiveRowNumber,
getColorCodeAtActiveRowNumberForUser,
getColorCodeAtCenterOfRainbow,
Expand All @@ -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) {
Expand All @@ -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),
];

/**
Expand All @@ -79,4 +70,4 @@ export function activate(context: vscode.ExtensionContext) {
/**
* deactivate extension
*/
export function deactivate() {}
export function deactivate() { }
Loading

0 comments on commit 2fc551f

Please sign in to comment.