-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code lens for editing files from settings
Add ability to open editor for file added to `dotfiles.files` settings using a code lens, to make it faster to update the files in settings.json. Changes made in the editor automatically update the file's content in settings.json if `dotfiles.autoUpdate` is enabled.
- Loading branch information
Showing
8 changed files
with
115 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,15 +8,19 @@ | |
}, | ||
"description": "Apply dotfiles from settings", | ||
"icon": "images/icon-small.png", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
"preview": true, | ||
"engines": { | ||
"vscode": "^1.74.0" | ||
}, | ||
"categories": [ | ||
"Other" | ||
], | ||
"author": { | ||
"name": "grg", | ||
"url": "https://grg.app", | ||
"email": "[email protected]" | ||
}, | ||
"homepage": "https://github.com/grgar/vscode-dotfiles", | ||
"qna": "https://github.com/grgar/vscode-dotfiles/discussions", | ||
"bugs": { | ||
|
@@ -68,14 +72,15 @@ | |
"test": "vscode-test" | ||
}, | ||
"devDependencies": { | ||
"@types/vscode": "^1.74.0", | ||
"@types/mocha": "^10.0.6", | ||
"@types/node": "18.x", | ||
"@types/vscode": "^1.74.0", | ||
"@typescript-eslint/eslint-plugin": "^7.0.2", | ||
"@typescript-eslint/parser": "^7.0.2", | ||
"eslint": "^8.56.0", | ||
"typescript": "^5.3.3", | ||
"@vscode/test-cli": "^0.0.6", | ||
"@vscode/test-electron": "^2.3.9" | ||
"@vscode/test-electron": "^2.3.9", | ||
"eslint": "^8.56.0", | ||
"jsonc-parser": "^3.2.1", | ||
"typescript": "^5.3.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { JSONVisitor, visit } from "jsonc-parser"; | ||
import path from "path"; | ||
import * as vscode from "vscode"; | ||
|
||
export class SettingsLensProvider implements vscode.CodeLensProvider { | ||
private key: string; | ||
private basePath: string; | ||
|
||
constructor(key: string, basePath: string) { | ||
this.key = key; | ||
this.basePath = basePath; | ||
} | ||
|
||
provideCodeLenses(document: vscode.TextDocument): vscode.ProviderResult<vscode.CodeLens[]> { | ||
return parseJSONForFileLocations(this.key, document).map( | ||
({ name, range }) => | ||
new vscode.CodeLens( | ||
range, | ||
{ | ||
title: "open file", | ||
command: "vscode.open", | ||
arguments: [path.join(this.basePath, name)], | ||
}, | ||
), | ||
); | ||
} | ||
} | ||
|
||
export interface FileLocation { | ||
name: string; | ||
range: vscode.Range; | ||
} | ||
|
||
const parseJSONForFileLocations = (filesProperty: String, document: vscode.TextDocument, buffer = document.getText()): FileLocation[] => { | ||
let level = 0; | ||
let inFiles = false; | ||
const files: FileLocation[] = []; | ||
const visitor: JSONVisitor = { | ||
onObjectBegin() { | ||
level++; | ||
}, | ||
onObjectEnd() { | ||
inFiles = false; | ||
level--; | ||
}, | ||
onObjectProperty(property: string, offset: number, length: number) { | ||
if (level === 1 && property === filesProperty) { | ||
inFiles = true; | ||
} else if (inFiles) { | ||
files.push({ | ||
name: property, | ||
range: new vscode.Range(document.positionAt(offset), document.positionAt(offset + length)) | ||
}); | ||
} | ||
}, | ||
}; | ||
visit(buffer, visitor); | ||
return files; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters