vscode#CodeActionKind TypeScript Examples

The following examples show how to use vscode#CodeActionKind. 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: ALDocumentationQuickFix.ts    From vscode-alxmldocumentation with MIT License 6 votes vote down vote up
/**
     * Add Code Action to fix Object XML Documentation missing.
     * @param alProcedure AL Procedure.
     * @param diagnostic Diagnostic entry.
     */
    private AddObjectDocumentationMissingCodeAction(alObject: ALObject | null, diagnostic: Diagnostic) {
        let action: CodeAction = new CodeAction('Add XML documentation', CodeActionKind.QuickFix);
        action.command = {
            command: `${ALXmlDocConfigurationPrefix}.fixObjectDocumentation`,
            title: 'Add object XML documentation',
            tooltip: `Automatically fix missing XML documentation for object ${alObject?.Name}.`,
            arguments: [alObject]
        };
        action.diagnostics = [diagnostic];
        action.isPreferred = true;
        this.QuickFixActions.push(action);
    }
Example #2
Source File: ALDocumentationQuickFix.ts    From vscode-alxmldocumentation with MIT License 6 votes vote down vote up
/**
     * Add Code Action to fix Procedure XML Documentation missing.
     * @param alProcedure AL Procedure.
     * @param diagnostic Diagnostic entry.
     */
    private AddDocumentationMissingCodeAction(alProcedure: ALProcedure | undefined, diagnostic: Diagnostic) {
        let action: CodeAction = new CodeAction('Add XML documentation', CodeActionKind.QuickFix);
        action.command = {
            command: `${ALXmlDocConfigurationPrefix}.fixDocumentation`,
            title: 'Add procedure XML documentation',
            tooltip: `Automatically fix missing XML documentation for procedure ${alProcedure?.Name}.`,
            arguments: [alProcedure]
        };
        action.diagnostics = [diagnostic];
        action.isPreferred = true;
        this.QuickFixActions.push(action);
    }
Example #3
Source File: ALDocumentationQuickFix.ts    From vscode-alxmldocumentation with MIT License 6 votes vote down vote up
/**
     * Add Code Action to fix Summary XML Documentation missing.
     * @param alProcedure AL Procedure.
     * @param diagnostic Diagnostic entry.
     */
    private AddSummaryDocumentationMissingCodeAction(alProcedure: ALProcedure | undefined, diagnostic: Diagnostic) {
        let action: CodeAction = new CodeAction('Add summary XML documentation', CodeActionKind.QuickFix);
        action.command = {
            command: `${ALXmlDocConfigurationPrefix}.fixSummaryDocumentation`,
            title: 'Add procedure summary XML documentation',
            tooltip: `Automatically fix missing summary XML documentation for procedure ${alProcedure?.Name}.`,
            arguments: [alProcedure]
        };
        action.diagnostics = [diagnostic];
        action.isPreferred = true;
        this.QuickFixActions.push(action);
    }
Example #4
Source File: ALDocumentationQuickFix.ts    From vscode-alxmldocumentation with MIT License 6 votes vote down vote up
/**
     * Add Code Action to fix Parameter XML Documentation missing.
     * @param alProcedure AL Procedure.
     * @param diagnostic Diagnostic entry.
     */
    private AddParameterDocumentationMissingCodeAction(alProcedure: ALProcedure | undefined, diagnostic: Diagnostic) {
        let action: CodeAction = new CodeAction('Add parameter XML documentation', CodeActionKind.QuickFix);
        action.command = {
            command: `${ALXmlDocConfigurationPrefix}.fixParameterDocumentation`,
            title: 'Add parameter XML documentation',
            tooltip: `Automatically fix missing parameter XML documentation for procedure ${alProcedure?.Name}.`,
            arguments: [alProcedure]
        };
        action.diagnostics = [diagnostic];
        action.isPreferred = true;
        this.QuickFixActions.push(action);
    }
Example #5
Source File: ALDocumentationQuickFix.ts    From vscode-alxmldocumentation with MIT License 6 votes vote down vote up
/**
     * Add Code Action to fix Return Value XML Documentation missing.
     * @param alProcedure AL Procedure.
     * @param diagnostic Diagnostic entry.
     */
    private AddReturnDocumentationMissingCodeAction(alProcedure: ALProcedure | undefined, diagnostic: Diagnostic) {
        let action: CodeAction = new CodeAction('Add return value XML documentation', CodeActionKind.QuickFix);
        action.command = {
            command: `${ALXmlDocConfigurationPrefix}.fixReturnDocumentation`,
            title: 'Add return value XML documentation',
            tooltip: `Automatically fix missing return value XML documentation for procedure ${alProcedure?.Name}.`,
            arguments: [alProcedure]
        };
        action.diagnostics = [diagnostic];
        action.isPreferred = true;
        this.QuickFixActions.push(action);
    }
Example #6
Source File: ALDocumentationQuickFix.ts    From vscode-alxmldocumentation with MIT License 6 votes vote down vote up
/**
     * Add Code Action to fix unnecessary Parameter XML Documentation missing.
     * @param alProcedure AL Procedure.
     * @param diagnostic Diagnostic entry.
     */
    private AddUnnecessaryParameterDocumentationMissingCodeAction(alProcedure: ALProcedure | undefined, diagnostic: Diagnostic) {
        let action: CodeAction = new CodeAction('Remove unnecessary parameter XML documentation', CodeActionKind.QuickFix);
        action.command = {
            command: `${ALXmlDocConfigurationPrefix}.fixUnnecessaryParameterDocumentation`,
            title: 'Remove unnecessary parameter XML documentation',
            tooltip: `Automatically fix unnecessary parameter XML documentation for procedure ${alProcedure?.Name}.`,
            arguments: [alProcedure, diagnostic.range]
        };
        action.diagnostics = [diagnostic];
        action.isPreferred = true;
        this.QuickFixActions.push(action);
    }
Example #7
Source File: codeActionProvider.ts    From dendron with GNU Affero General Public License v3.0 6 votes vote down vote up
doctorFrontmatterProvider: CodeActionProvider = {
  provideCodeActions: sentryReportingCallback(
    (
      _document: TextDocument,
      _range: Range | Selection,
      context: CodeActionContext,
      _token: CancellationToken
    ) => {
      // No-op if we're not in a Dendron Workspace
      if (!DendronExtension.isActive()) {
        return;
      }
      // Only provide fix frontmatter action if the diagnostic is correct
      const diagnostics = context.diagnostics.filter(
        (item) => item.code === BAD_FRONTMATTER_CODE
      );
      if (diagnostics.length !== 0) {
        const action: CodeAction = {
          title: "Fix the frontmatter",
          diagnostics,
          isPreferred: true,
          kind: CodeActionKind.QuickFix,
          command: {
            command: new DoctorCommand(ExtensionProvider.getExtension()).key,
            title: "Fix the frontmatter",
            arguments: [
              { scope: "file", action: DoctorActionsEnum.FIX_FRONTMATTER },
            ],
          },
        };
        return [action];
      }
      return undefined;
    }
  ),
}
Example #8
Source File: actions.ts    From format-imports-vscode with MIT License 5 votes vote down vote up
CODE_ACTIONS = [
  {
    title: 'Sort Imports/Exports',
    // Correspond to `SortActionProvider.ACTION_ID`
    kind: CodeActionKind.SourceOrganizeImports.append('sortImports'),
    command: 'tsImportSorter.command.sortImports',
  },
]
Example #9
Source File: actionProvider.ts    From typescript-explicit-types with GNU General Public License v3.0 5 votes vote down vote up
public static readonly fixAllCodeActionKind = CodeActionKind.SourceFixAll.append('tslint');
Example #10
Source File: actionProvider.ts    From typescript-explicit-types with GNU General Public License v3.0 5 votes vote down vote up
public static metadata: CodeActionProviderMetadata = {
    providedCodeActionKinds: [CodeActionKind.QuickFix],
  };
Example #11
Source File: ObjIdConfigCodeActionProvider.ts    From al-objid with MIT License 5 votes vote down vote up
function createAction(command: string, args: any[], title: string) {
    const action = new CodeAction(title);
    action.kind = CodeActionKind.QuickFix;
    action.command = { command, arguments: args, title };
    return action;
}
Example #12
Source File: codeActionProvider.ts    From dendron with GNU Affero General Public License v3.0 4 votes vote down vote up
refactorProvider: CodeActionProvider = {
  provideCodeActions: sentryReportingCallback(
    async (
      _document: TextDocument,
      _range: Range | Selection,
      _context: CodeActionContext,
      _token: CancellationToken
    ) => {
      // No-op if we're not in a Dendron Workspace
      const ext = ExtensionProvider.getExtension();
      if (!(await ext.isActiveAndIsDendronNote(_document.uri.fsPath))) {
        return;
      }

      const { editor, selection, text } = VSCodeUtils.getSelection();
      if (!editor || !selection) return;

      const header = getHeaderAt({
        document: editor.document,
        position: selection.start,
      });

      // action declaration
      const renameHeaderAction = {
        title: "Rename Header",
        isPreferred: true,
        kind: CodeActionKind.RefactorInline,
        command: {
          command: new RenameHeaderCommand().key,
          title: "Rename Header",
          arguments: [{ source: ContextualUIEvents.ContextualUICodeAction }],
        },
      };
      const brokenWikilinkAction = {
        title: "Add missing note for wikilink declaration",
        isPreferred: true,
        kind: CodeActionKind.RefactorExtract,
        command: {
          command: new GotoNoteCommand(ExtensionProvider.getExtension()).key,
          title: "Add missing note for wikilink declaration",
          arguments: [{ source: ContextualUIEvents.ContextualUICodeAction }],
        },
      };
      const createNewNoteAction = {
        title: "Extract text to new note",
        isPreferred: true,
        kind: CodeActionKind.RefactorExtract,
        command: {
          command: new NoteLookupCommand().key,
          title: "Extract text to new note",
          arguments: [
            {
              selectionType: LookupSelectionTypeEnum.selectionExtract,
              source: ContextualUIEvents.ContextualUICodeAction,
            },
          ],
        },
      };

      const copyHeaderRefAction = {
        title: "Copy Header Reference",
        isPreferred: true,
        kind: CodeActionKind.RefactorInline,
        command: {
          command: new CopyNoteRefCommand().key,
          title: "Copy Header Reference",
          arguments: [{ source: ContextualUIEvents.ContextualUICodeAction }],
        },
      };

      const WrapAsMarkdownLink = {
        title: "Wrap as Markdown Link",
        isPreferred: true,
        kind: CodeActionKind.RefactorInline,
        command: {
          command: new PasteLinkCommand().key,
          title: "Wrap as Markdown Link",
          arguments: [
            {
              source: ContextualUIEvents.ContextualUICodeAction,
              link: text,
              selection,
            },
          ],
        },
      };

      if (_range.isEmpty) {
        const { engine } = ext.getDWorkspace();
        const note = new WSUtilsV2(ext).getActiveNote();
        //return a code action for create note if user clicked next to a broken wikilink
        if (
          note &&
          (await isBrokenWikilink({ editor, engine, note, selection }))
        ) {
          return [brokenWikilinkAction];
        }

        //return a code action for rename header and copy header ref if user clicks next to a header
        if (!_.isUndefined(header)) {
          return [renameHeaderAction, copyHeaderRefAction];
        }
        // return if none
        return;
      } else {
        //regex for url
        if (!_.isUndefined(text) && isUrl(text)) {
          return [WrapAsMarkdownLink];
        }
        return !_.isUndefined(header)
          ? [createNewNoteAction, renameHeaderAction, copyHeaderRefAction]
          : [createNewNoteAction];
      }
    }
  ),
}
Example #13
Source File: actionProvider.ts    From typescript-explicit-types with GNU General Public License v3.0 4 votes vote down vote up
public async provideCodeActions(document: TextDocument, range: Range | Selection, context: CodeActionContext): Promise<CodeAction[]> {
    const config = workspace.getConfiguration(configurationId);
    const isPreferrable = config.get<boolean>(ConfigurationKey.preferable);

    const allDefinitions: Location[] = [];
    for (let lineNumber = range.start.line; lineNumber <= range.end.line; lineNumber++) {
      const line = document.lineAt(lineNumber);
      const startCharNumber = lineNumber === range.start.line ? range.start.character : 0;
      const endCharNumber = lineNumber === range.end.line ? range.end.character : line.range.end.character;
      for (let charNumber = startCharNumber; charNumber <= endCharNumber; charNumber++) {
        const foundDefinitions = await executeDefinitionProvider(document.uri, new Position(lineNumber, charNumber));
        if (foundDefinitions?.length) allDefinitions.push(foundDefinitions[0]);
      }
    }

    if (!allDefinitions.length) return [];

    const definitions = uniqWith(allDefinitions, (a, b) => a.originSelectionRange.isEqual(b.originSelectionRange));
    const symbols = await executeSymbolProvider(document.uri);

    const generateTypeInfos: GenerateTypeInfo[] = [];
    for (const definition of definitions) {
      const hoverRes = await executeHoverProvider(document.uri, definition.originSelectionRange.start);
      if (!hoverRes) continue;

      // check if definition has a typescript annotation
      const tsHoverContent = hoverRes
        .reduce<string[]>((acc, val) => acc.concat(val.contents.map((x) => x.value)), [])
        .find((x) => x.includes('typescript'));
      if (!tsHoverContent) continue;

      const word = document.getText(definition.originSelectionRange);
      const lineText = document.getText(document.lineAt(definition.originSelectionRange.start.line).range);

      // => is recognized as a definition, but it's type is usually defined before, unlike all other types
      if (word === '=>') {
        // check that there are arrow functions without type on this line
        const regexp = /\)\s*=>/gm;
        const matches = findMatches(regexp, lineText);
        const indexes = matches.map((x) => x.index);
        if (!matches.length) continue;

        const passedIndex = indexes.find((i) => i > definition.originSelectionRange.start.character);

        // look for a potential index of a match
        // there might be several arrow functions on the same line & this definition might actually be one with a type
        const potentialIndexIndex = passedIndex ? indexes.indexOf(passedIndex) - 1 : indexes.length - 1;
        if (potentialIndexIndex < 0) continue;

        // check that our match contains the definition
        const potentialIndex = indexes[potentialIndexIndex];
        const definitionMatch = matches![potentialIndexIndex];
        if (
          potentialIndex >= definition.originSelectionRange.start.character ||
          potentialIndex + definitionMatch[0].length <= definition.originSelectionRange.start.character
        )
          continue;

        generateTypeInfos.push({
          isFunction: true,
          typescriptHoverResult: tsHoverContent,
          typePosition: new Position(definition.originSelectionRange.start.line, potentialIndex + 1),
        });
        continue;
      }

      const symbol = symbols?.find((x) => x.selectionRange.contains(definition.originSelectionRange));
      const trailingSlice = document.getText(new Range(definition.originSelectionRange.end, definition.targetRange.end));
      const isFollowedByBracket = !!trailingSlice.match(/^(\s|\\[rn])*\(/);
      if (symbol?.kind === SymbolKind.Function || word === 'function' || isFollowedByBracket) {
        // find out suitable type position by looking for a closing bracket of the function
        const offset = document.offsetAt(definition.originSelectionRange.end);
        const firstBracket = trailingSlice.indexOf('(');
        const closingBracketIndex = findClosingBracketMatchIndex(trailingSlice, firstBracket);

        const isFunctionTyped = trailingSlice.slice(closingBracketIndex + 1).match(/^\s*:/);
        if (isFunctionTyped) continue;

        const definitionSlice = document.getText(definition.targetRange);
        const firstSymbol = definitionSlice.match(/^\w+/);

        generateTypeInfos.push({
          typescriptHoverResult: tsHoverContent,
          typePosition: document.positionAt(offset + closingBracketIndex + 1),
          isFunction: !firstSymbol || !functionHandlerExceptions.includes(firstSymbol[0]),
        });
      } else {
        // check if type annotation is already present
        const typePosition = new Position(definition.originSelectionRange.end.line, definition.originSelectionRange.end.character);
        const slice = lineText.slice(typePosition.character);
        const match = slice.match(/^\s*:/g);
        if (match?.length) continue;

        generateTypeInfos.push({
          typescriptHoverResult: tsHoverContent,
          typePosition,
        });
      }
    }

    if (!generateTypeInfos.length) return [];

    const action = new CodeAction('Generate explicit type', CodeActionKind.QuickFix);
    const args: Parameters<typeof commandHandler> = [generateTypeInfos];
    action.command = { command: 'extension.generateExplicitType', title: 'Generate explicit type', arguments: args };
    action.isPreferred = isPreferrable;

    return [action];
  }