vscode#CodeLensProvider TypeScript Examples

The following examples show how to use vscode#CodeLensProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: comment-lens-provider.ts    From vscode-code-review with MIT License 6 votes vote down vote up
export class CommentLensProvider implements CodeLensProvider {
  constructor(private exportFactory: ExportFactory) {}

  public provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
    return this.exportFactory.getFilesContainingComments().then((filesWithComments) => {
      const codeLenses: CodeLens[] = [];
      filesWithComments.forEach((el) => {
        if (document.fileName.endsWith(el.data.group)) {
          el.data.lines.forEach((csvEntry) => {
            const fileSection: ReviewFileExportSection = {
              group: csvEntry.filename,
              lines: el.data.lines,
            };
            const csvRef: CsvEntry | undefined = csvEntry;
            const prio = Number(csvEntry.priority); // be sure the value is a number
            const priorityString = prio
              ? ` | Priority: ${csvEntry.priority}${symbolForPriority(Number(csvEntry.priority))}`
              : '';
            const command: Command = {
              title: `Code Review: ${csvEntry.title}${priorityString}`,
              tooltip: csvEntry.comment,
              command: 'codeReview.openSelection',
              arguments: [fileSection, csvRef],
            };

            rangesFromStringDefinition(csvEntry.lines).forEach((range: Range) => {
              codeLenses.push(new CodeLens(range, command));
            });
          });
        }
      });
      return codeLenses;
    });
  }
  // public resolveCodeLens?(codeLens: CodeLens, token: CancellationToken): CodeLens | Thenable<CodeLens> {
  //   return new CodeLens(new Range(new Position(0, 0), new Position(1, 2)));
  // }
}
Example #2
Source File: codelens-provider.ts    From plugin-vscode with Apache License 2.0 5 votes vote down vote up
export class ExecutorCodeLensProvider implements CodeLensProvider {

    private _onDidChangeCodeLenses: EventEmitter<void> = new EventEmitter<void>();
    public readonly onDidChangeCodeLenses: Event<void> = this._onDidChangeCodeLenses.event;

    private ballerinaExtension: BallerinaExtension;

    constructor(extensionInstance: BallerinaExtension) {
        this.ballerinaExtension = extensionInstance;

        workspace.onDidChangeConfiguration(() => {
            this._onDidChangeCodeLenses.fire();
        });
        workspace.onDidOpenTextDocument(() => {
            this._onDidChangeCodeLenses.fire();
        });
        workspace.onDidChangeTextDocument(() => {
            this._onDidChangeCodeLenses.fire();
        });

        commands.registerCommand(SOURCE_DEBUG_COMMAND, async (...args: any[]) => {
            sendTelemetryEvent(this.ballerinaExtension, TM_EVENT_SOURCE_DEBUG_CODELENS, CMP_EXECUTOR_CODELENS);
            clearTerminal();
            commands.executeCommand(FOCUS_DEBUG_CONSOLE_COMMAND);
            startDebugging(window.activeTextEditor!.document.uri, false, this.ballerinaExtension.getBallerinaCmd(),
                this.ballerinaExtension.getBallerinaHome(), args);
        });

        commands.registerCommand(TEST_DEBUG_COMMAND, async (...args: any[]) => {
            sendTelemetryEvent(this.ballerinaExtension, TM_EVENT_TEST_DEBUG_CODELENS, CMP_EXECUTOR_CODELENS);
            clearTerminal();
            commands.executeCommand(FOCUS_DEBUG_CONSOLE_COMMAND);
            startDebugging(window.activeTextEditor!.document.uri, true, this.ballerinaExtension.getBallerinaCmd(),
                this.ballerinaExtension.getBallerinaHome(), args);
        });
    }

    provideCodeLenses(_document: TextDocument, _token: CancellationToken): ProviderResult<CodeLens[]> {
        let codeLenses: CodeLens[] = [];
        if (this.ballerinaExtension.langClient && window.activeTextEditor) {
            return this.getCodeLensList();
        }
        return codeLenses;
    }

    private async getCodeLensList() {
        let codeLenses: CodeLens[] = [];
        await this.ballerinaExtension.langClient!.getExecutorPositions({
            documentIdentifier: {
                uri: window.activeTextEditor!.document.uri.toString()
            }
        }).then(response => {
            if (response.executorPositions) {
                response.executorPositions.forEach(position => {
                    codeLenses.push(this.createCodeLens(position, EXEC_TYPE.RUN));
                    codeLenses.push(this.createCodeLens(position, EXEC_TYPE.DEBUG));
                });
            }
        });
        return codeLenses;
    }

    private createCodeLens(execPosition: ExecutorPosition, execType: EXEC_TYPE): CodeLens {
        const startLine = execPosition.range.startLine.line;
        const startColumn = execPosition.range.startLine.offset;
        const endLine = execPosition.range.endLine.line;
        const endColumn = execPosition.range.endLine.offset;
        const codeLens = new CodeLens(new Range(startLine, startColumn, endLine, endColumn));
        codeLens.command = {
            title: execType.toString(),
            tooltip: `${execType.toString()} ${execPosition.name}`,
            command: execPosition.kind === EXEC_POSITION_TYPE.SOURCE ? (execType === EXEC_TYPE.RUN ? PALETTE_COMMANDS.RUN :
                SOURCE_DEBUG_COMMAND) : (execType === EXEC_TYPE.RUN ? PALETTE_COMMANDS.TEST : TEST_DEBUG_COMMAND),
            arguments: execPosition.kind === EXEC_POSITION_TYPE.SOURCE ? [] : (execType === EXEC_TYPE.RUN ?
                [EXEC_ARG.TESTS, execPosition.name] : [execPosition.name])
        };
        return codeLens;
    }
}