Просмотр исходного кода

Improve vscode line number display for test files. (#6957)

In the Carbon vscode extension, in /testdata/ files with file splits,
add a line number column within the split next to the line number column
for the overall file line number.

Assisted-by: Gemini 3.1 Pro via Antigravity
Richard Smith 1 месяц назад
Родитель
Сommit
29a8b315d3
1 измененных файлов с 80 добавлено и 0 удалено
  1. 80 0
      utils/vscode/src/extension.ts

+ 80 - 0
utils/vscode/src/extension.ts

@@ -10,9 +10,15 @@
 
 import {
   workspace,
+  window,
   ExtensionContext,
   commands,
   WorkspaceConfiguration,
+  TextEditor,
+  Range,
+  DecorationOptions,
+  ThemeColor,
+  TextDocumentChangeEvent,
 } from 'vscode';
 
 import {
@@ -23,6 +29,59 @@ import {
 
 let client: LanguageClient;
 
+const splitLineNumberDecorationType = window.createTextEditorDecorationType({});
+
+function updateSplitLineNumbers(editor: TextEditor | undefined) {
+  if (!editor) {
+    return;
+  }
+  const document = editor.document;
+  if (!document.fileName.includes('/testdata/')) {
+    return;
+  }
+
+  const decorations: DecorationOptions[] = [];
+
+  // Find all splits
+  const splitStarts: number[] = [];
+  for (let i = 0; i < document.lineCount; i++) {
+    if (document.lineAt(i).text.startsWith('// ---')) {
+      splitStarts.push(i);
+    }
+  }
+
+  // Iterate through each split
+  for (let s = 0; s < splitStarts.length; s++) {
+    const startLine = splitStarts[s];
+    const endLine = s + 1 < splitStarts.length ? splitStarts[s + 1] : document.lineCount;
+    const splitLength = endLine - startLine - 1;
+    if (splitLength <= 0) {
+      continue;
+    }
+
+    const maxDigits = splitLength.toString().length;
+    const widthStr = `${maxDigits}ch`;
+
+    for (let i = startLine + 1; i < endLine; i++) {
+      const splitLineNum = i - startLine;
+      decorations.push({
+        range: new Range(i, 0, i, 0),
+        renderOptions: {
+          before: {
+            contentText: splitLineNum.toString(),
+            color: new ThemeColor('editorLineNumber.foreground'),
+            width: widthStr,
+            margin: '0 2ch 0 0',
+            textDecoration: 'none; text-align: right;',
+          }
+        }
+      });
+    }
+  }
+
+  editor.setDecorations(splitLineNumberDecorationType, decorations);
+}
+
 /**
  * Splits a CLI-style quoted string.
  */
@@ -136,6 +195,27 @@ export function activate(context: ExtensionContext) {
       client.restart();
     })
   );
+
+  // Update split line numbers when the active editor changes
+  context.subscriptions.push(
+    window.onDidChangeActiveTextEditor((editor: TextEditor | undefined) => {
+      updateSplitLineNumbers(editor);
+    })
+  );
+
+  // Update split line numbers when the document changes
+  context.subscriptions.push(
+    workspace.onDidChangeTextDocument((event: TextDocumentChangeEvent) => {
+      if (window.activeTextEditor && event.document === window.activeTextEditor.document) {
+        updateSplitLineNumbers(window.activeTextEditor);
+      }
+    })
+  );
+
+  // Initial update
+  if (window.activeTextEditor) {
+    updateSplitLineNumbers(window.activeTextEditor);
+  }
 }
 
 export function deactivate(): Thenable<void> | undefined {