|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as vscode from 'vscode' |
| 7 | +import { editorUtilities, setContext } from 'aws-core-vscode/shared' |
| 8 | + |
| 9 | +export interface LineSelection { |
| 10 | + anchor: number |
| 11 | + active: number |
| 12 | +} |
| 13 | + |
| 14 | +export interface LinesChangeEvent { |
| 15 | + readonly editor: vscode.TextEditor | undefined |
| 16 | + readonly selections: LineSelection[] | undefined |
| 17 | + |
| 18 | + readonly reason: 'editor' | 'selection' | 'content' |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * This class providees a single interface to manage and access users' "line" selections |
| 23 | + * Callers could use it by subscribing onDidChangeActiveLines to do UI updates or logic needed to be executed when line selections get changed |
| 24 | + */ |
| 25 | +export class LineTracker implements vscode.Disposable { |
| 26 | + private _onDidChangeActiveLines = new vscode.EventEmitter<LinesChangeEvent>() |
| 27 | + get onDidChangeActiveLines(): vscode.Event<LinesChangeEvent> { |
| 28 | + return this._onDidChangeActiveLines.event |
| 29 | + } |
| 30 | + |
| 31 | + private _editor: vscode.TextEditor | undefined |
| 32 | + private _disposable: vscode.Disposable | undefined |
| 33 | + |
| 34 | + private _selections: LineSelection[] | undefined |
| 35 | + get selections(): LineSelection[] | undefined { |
| 36 | + return this._selections |
| 37 | + } |
| 38 | + |
| 39 | + private _onReady: vscode.EventEmitter<void> = new vscode.EventEmitter<void>() |
| 40 | + get onReady(): vscode.Event<void> { |
| 41 | + return this._onReady.event |
| 42 | + } |
| 43 | + |
| 44 | + private _ready: boolean = false |
| 45 | + get isReady() { |
| 46 | + return this._ready |
| 47 | + } |
| 48 | + |
| 49 | + constructor() { |
| 50 | + this._disposable = vscode.Disposable.from( |
| 51 | + vscode.window.onDidChangeActiveTextEditor(async (e) => { |
| 52 | + await this.onActiveTextEditorChanged(e) |
| 53 | + }), |
| 54 | + vscode.window.onDidChangeTextEditorSelection(async (e) => { |
| 55 | + await this.onTextEditorSelectionChanged(e) |
| 56 | + }), |
| 57 | + vscode.workspace.onDidChangeTextDocument((e) => { |
| 58 | + this.onContentChanged(e) |
| 59 | + }) |
| 60 | + ) |
| 61 | + |
| 62 | + queueMicrotask(async () => await this.onActiveTextEditorChanged(vscode.window.activeTextEditor)) |
| 63 | + } |
| 64 | + |
| 65 | + dispose() { |
| 66 | + this._disposable?.dispose() |
| 67 | + } |
| 68 | + |
| 69 | + ready() { |
| 70 | + if (this._ready) { |
| 71 | + throw new Error('Linetracker is already activated') |
| 72 | + } |
| 73 | + |
| 74 | + this._ready = true |
| 75 | + queueMicrotask(() => this._onReady.fire()) |
| 76 | + } |
| 77 | + |
| 78 | + // @VisibleForTesting |
| 79 | + async onActiveTextEditorChanged(editor: vscode.TextEditor | undefined) { |
| 80 | + if (editor === this._editor) { |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + this._editor = editor |
| 85 | + this._selections = toLineSelections(editor?.selections) |
| 86 | + if (this._selections && this._selections[0]) { |
| 87 | + const s = this._selections.map((item) => item.active + 1) |
| 88 | + await setContext('codewhisperer.activeLine', s) |
| 89 | + } |
| 90 | + |
| 91 | + this.notifyLinesChanged('editor') |
| 92 | + } |
| 93 | + |
| 94 | + // @VisibleForTesting |
| 95 | + async onTextEditorSelectionChanged(e: vscode.TextEditorSelectionChangeEvent) { |
| 96 | + // If this isn't for our cached editor and its not a real editor -- kick out |
| 97 | + if (this._editor !== e.textEditor && !editorUtilities.isTextEditor(e.textEditor)) { |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + const selections = toLineSelections(e.selections) |
| 102 | + if (this._editor === e.textEditor && this.includes(selections)) { |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + this._editor = e.textEditor |
| 107 | + this._selections = selections |
| 108 | + if (this._selections && this._selections[0]) { |
| 109 | + const s = this._selections.map((item) => item.active + 1) |
| 110 | + await setContext('codewhisperer.activeLine', s) |
| 111 | + } |
| 112 | + |
| 113 | + this.notifyLinesChanged('selection') |
| 114 | + } |
| 115 | + |
| 116 | + // @VisibleForTesting |
| 117 | + onContentChanged(e: vscode.TextDocumentChangeEvent) { |
| 118 | + const editor = vscode.window.activeTextEditor |
| 119 | + if (e.document === editor?.document && e.contentChanges.length > 0 && editorUtilities.isTextEditor(editor)) { |
| 120 | + this._editor = editor |
| 121 | + this._selections = toLineSelections(this._editor?.selections) |
| 122 | + |
| 123 | + this.notifyLinesChanged('content') |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + notifyLinesChanged(reason: 'editor' | 'selection' | 'content') { |
| 128 | + const e: LinesChangeEvent = { editor: this._editor, selections: this.selections, reason: reason } |
| 129 | + this._onDidChangeActiveLines.fire(e) |
| 130 | + } |
| 131 | + |
| 132 | + includes(selections: LineSelection[]): boolean |
| 133 | + includes(line: number, options?: { activeOnly: boolean }): boolean |
| 134 | + includes(lineOrSelections: number | LineSelection[], options?: { activeOnly: boolean }): boolean { |
| 135 | + if (typeof lineOrSelections !== 'number') { |
| 136 | + return isIncluded(lineOrSelections, this._selections) |
| 137 | + } |
| 138 | + |
| 139 | + if (this._selections === undefined || this._selections.length === 0) { |
| 140 | + return false |
| 141 | + } |
| 142 | + |
| 143 | + const line = lineOrSelections |
| 144 | + const activeOnly = options?.activeOnly ?? true |
| 145 | + |
| 146 | + for (const selection of this._selections) { |
| 147 | + if ( |
| 148 | + line === selection.active || |
| 149 | + (!activeOnly && |
| 150 | + ((selection.anchor >= line && line >= selection.active) || |
| 151 | + (selection.active >= line && line >= selection.anchor))) |
| 152 | + ) { |
| 153 | + return true |
| 154 | + } |
| 155 | + } |
| 156 | + return false |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +function isIncluded(selections: LineSelection[] | undefined, within: LineSelection[] | undefined): boolean { |
| 161 | + if (selections === undefined && within === undefined) { |
| 162 | + return true |
| 163 | + } |
| 164 | + if (selections === undefined || within === undefined || selections.length !== within.length) { |
| 165 | + return false |
| 166 | + } |
| 167 | + |
| 168 | + return selections.every((s, i) => { |
| 169 | + const match = within[i] |
| 170 | + return s.active === match.active && s.anchor === match.anchor |
| 171 | + }) |
| 172 | +} |
| 173 | + |
| 174 | +function toLineSelections(selections: readonly vscode.Selection[]): LineSelection[] |
| 175 | +function toLineSelections(selections: readonly vscode.Selection[] | undefined): LineSelection[] | undefined |
| 176 | +function toLineSelections(selections: readonly vscode.Selection[] | undefined) { |
| 177 | + return selections?.map((s) => ({ active: s.active.line, anchor: s.anchor.line })) |
| 178 | +} |
0 commit comments