Skip to content

Commit

Permalink
feat: Add VSCode code lens to show the unit file type and provide a w…
Browse files Browse the repository at this point in the history
…ay to change it
  • Loading branch information
hangxingliu committed Feb 22, 2024
1 parent 65921dc commit b4dacf0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { vscodeConfigNS } from "./config/vscode-config";
import { HintDataManagers } from "./hint-data/manager/multiple";
import { SystemdLint } from "./vscode-lint";
import { SystemdCommands } from "./vscode-commands";
import { SystemdCodeLens } from "./vscode-codelens";
import { SystemdDocumentManager } from "./vscode-documents";

export function activate(context: ExtensionContext) {
Expand All @@ -23,6 +24,7 @@ export function activate(context: ExtensionContext) {
const completion = new SystemdCompletionProvider(config, hintDataManager);
const signature = new SystemdSignatureProvider(hintDataManager);
const lint = new SystemdLint(config, hintDataManager);
const codeLens = new SystemdCodeLens(config, hintDataManager);
const commands = new SystemdCommands();

subs.push(
Expand All @@ -38,6 +40,7 @@ export function activate(context: ExtensionContext) {
subs.push(signature.register());
subs.push(languages.registerHoverProvider(selector, signature));
subs.push(languages.registerCodeActionsProvider(selector, lint));
subs.push(languages.registerCodeLensProvider(selector, codeLens));
subs.push(
workspace.onDidOpenTextDocument((doc) => {
if (docs.onDidOpenTextDocument(doc)) lint.onDidOpenTextDocument(doc);
Expand Down
22 changes: 22 additions & 0 deletions src/vscode-codelens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { TextDocument, CancellationToken, CodeLensProvider, CodeLens } from "vscode";
import { ExtensionConfig } from "./config/vscode-config-loader";
import { HintDataManagers } from "./hint-data/manager/multiple";
import { systemdFileTypeNames } from "./parser/file-info";
import { SystemdCommands } from "./vscode-commands";
import { SystemdDocumentManager } from "./vscode-documents";

export class SystemdCodeLens implements CodeLensProvider {
constructor(private config: ExtensionConfig, private readonly managers: HintDataManagers) {}

provideCodeLenses(document: TextDocument, token: CancellationToken) {
const manager = SystemdDocumentManager.instance;
const fileType = manager.getType(document);
const codeLens = new CodeLens(
document.lineAt(0).range,
SystemdCommands.get("changeUnitFileType", "Systemd Unit Type: " + systemdFileTypeNames[fileType], [
document,
])
);
return [codeLens];
}
}

0 comments on commit b4dacf0

Please sign in to comment.