|
| 1 | +import { |
| 2 | + CodeLens, |
| 3 | + CodeLensProvider, |
| 4 | + Event, |
| 5 | + EventEmitter, |
| 6 | + Position, |
| 7 | + ProviderResult, |
| 8 | + TextDocument, |
| 9 | + workspace, |
| 10 | +} from "vscode"; |
| 11 | +import { nearestDartFrogProject } from "../utils"; |
| 12 | +import path = require("path"); |
| 13 | + |
| 14 | +abstract class ConfigurableCodeLensProvider implements CodeLensProvider { |
| 15 | + protected codeLenses: CodeLens[] = []; |
| 16 | + |
| 17 | + protected _onDidChangeCodeLenses: EventEmitter<void> = |
| 18 | + new EventEmitter<void>(); |
| 19 | + public readonly onDidChangeCodeLenses: Event<void> = |
| 20 | + this._onDidChangeCodeLenses.event; |
| 21 | + |
| 22 | + constructor() { |
| 23 | + workspace.onDidChangeConfiguration(() => { |
| 24 | + this._onDidChangeCodeLenses.fire(); |
| 25 | + }); |
| 26 | + } |
| 27 | + |
| 28 | + // eslint-disable-next-line no-unused-vars |
| 29 | + public provideCodeLenses(document: TextDocument): ProviderResult<CodeLens[]> { |
| 30 | + if (!this.hasEnabledCodeLenses()) { |
| 31 | + return undefined; |
| 32 | + } |
| 33 | + return this.codeLenses; |
| 34 | + } |
| 35 | + |
| 36 | + public resolveCodeLens?(codeLens: CodeLens): ProviderResult<CodeLens> { |
| 37 | + if (!this.hasEnabledCodeLenses()) { |
| 38 | + return undefined; |
| 39 | + } |
| 40 | + return codeLens; |
| 41 | + } |
| 42 | + |
| 43 | + private hasEnabledCodeLenses(): boolean { |
| 44 | + return workspace.getConfiguration("dart-frog").get("enableCodeLens", true); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// eslint-disable-next-line max-len |
| 49 | +abstract class RegularExpressionCodeLensProvider extends ConfigurableCodeLensProvider { |
| 50 | + abstract readonly regex: RegExp; |
| 51 | + |
| 52 | + public provideCodeLenses(document: TextDocument): ProviderResult<CodeLens[]> { |
| 53 | + if (!super.provideCodeLenses(document)) { |
| 54 | + return undefined; |
| 55 | + } |
| 56 | + |
| 57 | + this.codeLenses = []; |
| 58 | + const text = document.getText(); |
| 59 | + let matches; |
| 60 | + while ((matches = this.regex.exec(text)) !== null) { |
| 61 | + const line = document.lineAt(document.positionAt(matches.index).line); |
| 62 | + const indexOf = line.text.indexOf(matches[0]); |
| 63 | + const position = new Position(line.lineNumber, indexOf); |
| 64 | + const range = document.getWordRangeAtPosition( |
| 65 | + position, |
| 66 | + new RegExp(this.regex) |
| 67 | + ); |
| 68 | + if (range) { |
| 69 | + this.codeLenses.push(new CodeLens(range)); |
| 70 | + } |
| 71 | + } |
| 72 | + return this.codeLenses; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// eslint-disable-next-line max-len |
| 77 | +abstract class OnRequestCodeLensProvider extends RegularExpressionCodeLensProvider { |
| 78 | + readonly regex: RegExp = /Response\s*onRequest\(RequestContext .*?\)\s*{/g; |
| 79 | + |
| 80 | + public provideCodeLenses(document: TextDocument): ProviderResult<CodeLens[]> { |
| 81 | + if (document.languageId !== "dart") { |
| 82 | + return undefined; |
| 83 | + } |
| 84 | + |
| 85 | + const dartFrogProjectPath = nearestDartFrogProject(document.uri.fsPath); |
| 86 | + if (!dartFrogProjectPath) { |
| 87 | + return undefined; |
| 88 | + } |
| 89 | + |
| 90 | + const routesPath = path.join(dartFrogProjectPath, "routes"); |
| 91 | + if (!document.uri.fsPath.startsWith(routesPath)) { |
| 92 | + return undefined; |
| 93 | + } |
| 94 | + |
| 95 | + return super.provideCodeLenses(document); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Shows a "Run" CodeLens on route handlers, which allows starting a development |
| 101 | + * server. |
| 102 | + */ |
| 103 | +export class RunOnRequestCodeLensProvider extends OnRequestCodeLensProvider { |
| 104 | + public resolveCodeLens?(codeLens: CodeLens): ProviderResult<CodeLens> { |
| 105 | + if (!super.resolveCodeLens!(codeLens)) { |
| 106 | + return undefined; |
| 107 | + } |
| 108 | + |
| 109 | + codeLens.command = { |
| 110 | + title: "Run", |
| 111 | + tooltip: "Starts a development server", |
| 112 | + command: "dart-frog.start-dev-server", |
| 113 | + }; |
| 114 | + return codeLens; |
| 115 | + } |
| 116 | +} |
0 commit comments