@kkdev92/vscode-ext-kit - v4.1.1
    Preparing search index...

    Interface ActiveEditor

    Everything an extension does to the editor the user is looking at.

    Obtained from EditorService.active, so the "is there an editor?" question is answered once, at the top of a handler, instead of by every function separately.

    interface ActiveEditor {
        languageId: string;
        lineCount: number;
        selections: readonly TextRange[];
        currentLine(): string;
        edit(
            edits: readonly TextEdit[],
            options?: TextEditOptions,
        ): Promise<boolean>;
        editStages(stages: readonly EditStage[]): Promise<boolean>;
        insertAtCursor(text: string): Promise<boolean>;
        line(line: number): string;
        location(): DocumentLocation | undefined;
        moveCursor(position: TextPosition): void;
        offsetsAt(
            positions: readonly TextPosition[],
            signal?: AbortSignal,
        ): readonly number[];
        positionsAt(
            offsets: readonly number[],
            signal?: AbortSignal,
        ): readonly TextPosition[];
        rangeOfOffsets(startOffset: number, endOffset: number): TextRange;
        replace(range: TextRange, text: string): Promise<boolean>;
        select(range: TextRange): void;
        selectedText(): string;
        selectedTexts(): readonly string[];
        selectLine(line: number): void;
        selectWord(pattern?: RegExp): boolean;
        text(range?: TextRange): string;
        textOfOffsets(startOffset: number, endOffset: number): string;
        transformSelection(transform: (text: string) => string): Promise<boolean>;
        transformSelections(
            transform: (text: string, index: number) => string,
        ): Promise<boolean>;
    }
    Index
    languageId: string

    The document's language id, e.g. 'typescript'.

    lineCount: number

    Number of lines in the document.

    selections: readonly TextRange[]

    The current selections, primary first.

    • Replaces spans, as one undo step.

      The whole batch is resolved against the document as it stands when the call starts, so the ranges do not shift out from under each other. Resolves false when the platform refuses the edit and rejects on an adapter failure.

      Parameters

      Returns Promise<boolean>

      await editor.edit([{ range, text: 'replacement' }]);
      
    • Runs stages in order, as a single undo step.

      Each stage is handed the editor after the previous stage landed, which is the difference from ActiveEditor.edit: one batch resolves every replacement against the original document, so it cannot express "sort the lines, then dedupe what sorting produced".

      Without this, a three-stage pipeline is either three undos or three edits computed against a document that no longer looks like that. Stops and resolves false at the first refused stage; a thrown/rejected stage or adapter failure rejects.

      Parameters

      Returns Promise<boolean>

      await editor.editStages(
      pipeline.map((stage) => (current) =>
      current.selections.map((range) => ({ range, text: stage(current.text(range)) }))
      )
      );
    • Resolves many positions to offsets in a single pass — the inverse of ActiveEditor.positionsAt. Out-of-range lines clamp; out-of-range characters do not, so pass positions already valid for the document.

      Parameters

      • positions: readonly TextPosition[]
      • Optionalsignal: AbortSignal

      Returns readonly number[]

    • Resolves many offsets to positions in a single pass over the document, rather than one lookup each. Out-of-range offsets clamp to the document.

      signal is checked between lookups so a large batch stops promptly.

      Parameters

      • offsets: readonly number[]
      • Optionalsignal: AbortSignal

      Returns readonly TextPosition[]

      const offsets = [...text.matchAll(/TODO:/g)].map((m) => m.index);
      const positions = editor.positionsAt(offsets, context.signal);
    • Builds a span from two document offsets — the shape a regex match gives you.

      Parameters

      • startOffset: number
      • endOffset: number

      Returns TextRange

      const match = /TODO:/.exec(editor.text());
      const range = editor.rangeOfOffsets(match.index, match.index + match[0].length);
    • The primary selection's text, or '' when nothing is selected.

      Returns string

      const word = editor.selectedText();
      
    • Selects the word at the cursor, using the platform's word definition unless pattern overrides it. False when the cursor is not on a word.

      Parameters

      • Optionalpattern: RegExp

      Returns boolean

      if (editor.selectWord(/[\w-]+/)) {
      const kebab = editor.selectedText();
      }
    • The text between two document offsets.

      Parameters

      • startOffset: number
      • endOffset: number

      Returns string

    • Rewrites the primary selection. False when nothing is selected, so a command can bail without checking first.

      Parameters

      • transform: (text: string) => string

      Returns Promise<boolean>

      await editor.transformSelection((text) => text.toUpperCase());
      
    • Rewrites every non-empty selection as one undo step. False when nothing is selected.

      Parameters

      • transform: (text: string, index: number) => string

      Returns Promise<boolean>