|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import { |
| 3 | + LanguageClient, |
| 4 | + RequestType, |
| 5 | + type Position, |
| 6 | +} from 'vscode-languageclient/node'; |
| 7 | +import {positionLiteralToVSCodePosition, positionsToRange} from './mapping'; |
| 8 | + |
| 9 | +export type AutoDepsDecorationsLSPEvent = { |
| 10 | + useEffectCallExpr: [Position, Position]; |
| 11 | + decorations: Array<[Position, Position]>; |
| 12 | +}; |
| 13 | + |
| 14 | +export interface AutoDepsDecorationsParams { |
| 15 | + position: Position; |
| 16 | +} |
| 17 | + |
| 18 | +export namespace AutoDepsDecorationsRequest { |
| 19 | + export const type = new RequestType< |
| 20 | + AutoDepsDecorationsParams, |
| 21 | + AutoDepsDecorationsLSPEvent | null, |
| 22 | + void |
| 23 | + >('react/autodeps_decorations'); |
| 24 | +} |
| 25 | + |
| 26 | +const inferredEffectDepDecoration = |
| 27 | + vscode.window.createTextEditorDecorationType({ |
| 28 | + // TODO: make configurable? |
| 29 | + borderColor: new vscode.ThemeColor('diffEditor.move.border'), |
| 30 | + borderStyle: 'solid', |
| 31 | + borderWidth: '0 0 4px 0', |
| 32 | + }); |
| 33 | + |
| 34 | +let currentlyDecoratedAutoDepFnLoc: vscode.Range | null = null; |
| 35 | +export function getCurrentlyDecoratedAutoDepFnLoc(): vscode.Range | null { |
| 36 | + return currentlyDecoratedAutoDepFnLoc; |
| 37 | +} |
| 38 | +export function setCurrentlyDecoratedAutoDepFnLoc(range: vscode.Range): void { |
| 39 | + currentlyDecoratedAutoDepFnLoc = range; |
| 40 | +} |
| 41 | +export function clearCurrentlyDecoratedAutoDepFnLoc(): void { |
| 42 | + currentlyDecoratedAutoDepFnLoc = null; |
| 43 | +} |
| 44 | + |
| 45 | +let decorationRequestId = 0; |
| 46 | +export type AutoDepsDecorationsOptions = { |
| 47 | + shouldUpdateCurrent: boolean; |
| 48 | +}; |
| 49 | +export function requestAutoDepsDecorations( |
| 50 | + client: LanguageClient, |
| 51 | + position: vscode.Position, |
| 52 | + options: AutoDepsDecorationsOptions, |
| 53 | +) { |
| 54 | + const id = ++decorationRequestId; |
| 55 | + client |
| 56 | + .sendRequest(AutoDepsDecorationsRequest.type, {position}) |
| 57 | + .then(response => { |
| 58 | + if (response !== null) { |
| 59 | + const { |
| 60 | + decorations, |
| 61 | + useEffectCallExpr: [start, end], |
| 62 | + } = response; |
| 63 | + // Maintain ordering |
| 64 | + if (decorationRequestId === id) { |
| 65 | + if (options.shouldUpdateCurrent) { |
| 66 | + setCurrentlyDecoratedAutoDepFnLoc(positionsToRange(start, end)); |
| 67 | + } |
| 68 | + drawInferredEffectDepDecorations(decorations); |
| 69 | + } |
| 70 | + } else { |
| 71 | + clearCurrentlyDecoratedAutoDepFnLoc(); |
| 72 | + clearDecorations(inferredEffectDepDecoration); |
| 73 | + } |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +export function drawInferredEffectDepDecorations( |
| 78 | + decorations: Array<[Position, Position]>, |
| 79 | +): void { |
| 80 | + const decorationOptions = decorations.map(([start, end]) => { |
| 81 | + return { |
| 82 | + range: new vscode.Range( |
| 83 | + positionLiteralToVSCodePosition(start), |
| 84 | + positionLiteralToVSCodePosition(end), |
| 85 | + ), |
| 86 | + hoverMessage: 'Inferred as an effect dependency', |
| 87 | + }; |
| 88 | + }); |
| 89 | + vscode.window.activeTextEditor?.setDecorations( |
| 90 | + inferredEffectDepDecoration, |
| 91 | + decorationOptions, |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +export function clearDecorations( |
| 96 | + decorationType: vscode.TextEditorDecorationType, |
| 97 | +) { |
| 98 | + vscode.window.activeTextEditor?.setDecorations(decorationType, []); |
| 99 | +} |
0 commit comments