Java Code Examples for org.eclipse.lsp4j.CodeAction#setTitle()

The following examples show how to use org.eclipse.lsp4j.CodeAction#setTitle() . 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: CodeActionAcceptor.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/** Adds a quick-fix code action with the given title, edit and command */
public void acceptQuickfixCodeAction(QuickfixContext context, String title, WorkspaceEdit edit, Command command) {
	if (edit == null && command == null) {
		return;
	}
	CodeAction codeAction = new CodeAction();
	codeAction.setTitle(title);
	codeAction.setEdit(edit);
	codeAction.setCommand(command);
	codeAction.setKind(CodeActionKind.QuickFix);
	if (context.options != null && context.options.getCodeActionParams() != null) {
		CodeActionContext cac = context.options.getCodeActionParams().getContext();
		if (cac != null && cac.getDiagnostics() != null) {
			codeAction.setDiagnostics(cac.getDiagnostics());
		}
	}
	codeActions.add(Either.forRight(codeAction));
}
 
Example 2
Source File: CodeActionsUtils.java    From vscode-as3mxml with Apache License 2.0 6 votes vote down vote up
private static void createCodeActionsForGenerateGetterAndSetter(IVariableNode variableNode, String uri, String fileText, Range codeActionsRange, List<Either<Command, CodeAction>> codeActions)
{
    WorkspaceEdit getSetEdit = createWorkspaceEditForGenerateGetterAndSetter(
        variableNode, uri, fileText, true, true);
    CodeAction getAndSetCodeAction = new CodeAction();
    getAndSetCodeAction.setTitle("Generate 'get' and 'set' accessors");
    getAndSetCodeAction.setEdit(getSetEdit);
    getAndSetCodeAction.setKind(CodeActionKind.RefactorRewrite);
    codeActions.add(Either.forRight(getAndSetCodeAction));
    
    WorkspaceEdit getterEdit = createWorkspaceEditForGenerateGetterAndSetter(
        variableNode, uri, fileText, true, false);
    CodeAction getterCodeAction = new CodeAction();
    getterCodeAction.setTitle("Generate 'get' accessor (make read-only)");
    getterCodeAction.setEdit(getterEdit);
    getterCodeAction.setKind(CodeActionKind.RefactorRewrite);
    codeActions.add(Either.forRight(getterCodeAction));

    WorkspaceEdit setterEdit = createWorkspaceEditForGenerateGetterAndSetter(
        variableNode, uri, fileText, false, true);
    CodeAction setterCodeAction = new CodeAction();
    setterCodeAction.setTitle("Generate 'set' accessor (make write-only)");
    setterCodeAction.setEdit(setterEdit);
    setterCodeAction.setKind(CodeActionKind.RefactorRewrite);
    codeActions.add(Either.forRight(setterCodeAction));
}
 
Example 3
Source File: CodeActionProvider.java    From vscode-as3mxml with Apache License 2.0 6 votes vote down vote up
private void findSourceActions(Path path, List<Either<Command, CodeAction>> codeActions)
{
    Command organizeCommand = new Command();
    organizeCommand.setTitle("Organize Imports");
    organizeCommand.setCommand(ICommandConstants.ORGANIZE_IMPORTS_IN_URI);
    JsonObject uri = new JsonObject();
    uri.addProperty("external", path.toUri().toString());
    organizeCommand.setArguments(Lists.newArrayList(
        uri
    ));
    CodeAction organizeImports = new CodeAction();
    organizeImports.setKind(CodeActionKind.SourceOrganizeImports);
    organizeImports.setTitle(organizeCommand.getTitle());
    organizeImports.setCommand(organizeCommand);
    codeActions.add(Either.forRight(organizeImports));
}
 
Example 4
Source File: CodeActionProvider.java    From vscode-as3mxml with Apache License 2.0 6 votes vote down vote up
private void createCodeActionsForUnusedImport(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions)
{
    String fileText = fileTracker.getText(path);
    if(fileText == null)
    {
        return;
    }

    Range range = diagnostic.getRange();
    WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForRemoveUnusedImport(fileText, path.toUri().toString(), diagnostic.getRange());
    if (edit == null)
    {
        return;
    }

    int startOffset = LanguageServerCompilerUtils.getOffsetFromPosition(new StringReader(fileText), range.getStart());
    int endOffset = LanguageServerCompilerUtils.getOffsetFromPosition(new StringReader(fileText), range.getEnd());

    String importText = fileText.substring(startOffset, endOffset);
    CodeAction codeAction = new CodeAction();
    codeAction.setTitle("Remove " + importText);
    codeAction.setEdit(edit);
    codeAction.setKind(CodeActionKind.QuickFix);
    codeAction.setDiagnostics(Collections.singletonList(diagnostic));
    codeActions.add(Either.forRight(codeAction));
}
 
Example 5
Source File: XMLAssert.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
public static CodeAction ca(Diagnostic d, TextEdit... te) {
	CodeAction codeAction = new CodeAction();
	codeAction.setTitle("");
	codeAction.setDiagnostics(Arrays.asList(d));

	VersionedTextDocumentIdentifier versionedTextDocumentIdentifier = new VersionedTextDocumentIdentifier(FILE_URI,
			0);

	TextDocumentEdit textDocumentEdit = new TextDocumentEdit(versionedTextDocumentIdentifier, Arrays.asList(te));
	WorkspaceEdit workspaceEdit = new WorkspaceEdit(Collections.singletonList(Either.forLeft(textDocumentEdit)));
	codeAction.setEdit(workspaceEdit);
	return codeAction;
}
 
Example 6
Source File: XMLAssert.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
public static CodeAction ca(Diagnostic d, Either<TextDocumentEdit, ResourceOperation>... ops) {
	CodeAction codeAction = new CodeAction();
	codeAction.setDiagnostics(Collections.singletonList(d));
	codeAction.setEdit(new WorkspaceEdit(Arrays.asList(ops)));
	codeAction.setTitle("");
	return codeAction;
}
 
Example 7
Source File: CodeActionAcceptor.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void acceptSourceAction(String title, String kind, String commandId, Object... arguments) {
	if (!CodeActionUtils.isSpecialKindOf(kind, CodeActionKind.Source)) {
		throw new IllegalArgumentException("not a source action kind: " + kind);
	}
	CodeAction codeAction = new CodeAction();
	codeAction.setTitle(title);
	codeAction.setKind(kind);
	Command command = new Command(title, commandId, Arrays.asList(arguments));
	codeAction.setCommand(command);
	codeActions.add(Either.forRight(codeAction));
}
 
Example 8
Source File: CodeActionProvider.java    From vscode-as3mxml with Apache License 2.0 5 votes vote down vote up
private void createCodeActionForMissingCatchOrFinally(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions)
  {
      ILspProject project = folderData.project;
      Position position = diagnostic.getRange().getStart();
      IncludeFileData includeFileData = folderData.includedFiles.get(path.toString());
int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData);
      IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData);
      if(!(offsetNode instanceof ITryNode))
      {
          return;
      }
      ITryNode tryNode = (ITryNode) offsetNode;
      String fileText = fileTracker.getText(path);
      if(fileText == null)
      {
          return;
      }

      WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForGenerateCatch(
          tryNode, path.toUri().toString(), fileText, project);
      if(edit == null)
      {
          return;
      }

      CodeAction codeAction = new CodeAction();
      codeAction.setDiagnostics(Collections.singletonList(diagnostic));
      codeAction.setTitle("Generate catch");
      codeAction.setEdit(edit);
      codeAction.setKind(CodeActionKind.QuickFix);
      codeActions.add(Either.forRight(codeAction));
  }
 
Example 9
Source File: CodeActionService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private CodeAction fixUnsortedMembers(Diagnostic d, ICodeActionService2.Options options) {
	WorkspaceEdit wsEdit = recordWorkspaceEdit(options, (Resource copiedResource) -> {
		Model model = Iterables.getFirst(Iterables.filter(copiedResource.getContents(), Model.class), null);
		if (model != null) {
			for (TypeDeclaration type : Iterables.filter(model.getElements(), TypeDeclaration.class)) {
				ECollections.sort(type.getMembers(), (Member a, Member b) -> a.getName().compareTo(b.getName()));
			}
		}
	});
	CodeAction codeAction = new CodeAction();
	codeAction.setTitle("Sort Members");
	codeAction.setDiagnostics(Lists.newArrayList(d));
	codeAction.setEdit(wsEdit);
	return codeAction;
}
 
Example 10
Source File: NonNullTest.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCodeAction2() {
	try {
		CodeAction codeAction = new CodeAction();
		codeAction.setTitle(null);
		fail("Expected an IllegalArgumentException");
	} catch (IllegalArgumentException exc) {
		assertEquals("Property must not be null: title", exc.getMessage());
	}
}
 
Example 11
Source File: CodeActionProvider.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
private void createCodeActionForMissingField(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions)
  {
      Position position = diagnostic.getRange().getStart();
      IncludeFileData includeFileData = folderData.includedFiles.get(path.toString());
int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData);
      IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData);
      if (offsetNode instanceof IMXMLInstanceNode)
      {
          MXMLData mxmlData = workspaceFolderManager.getMXMLDataForPath(path, folderData);
          if (mxmlData != null)
          {
              IMXMLTagData offsetTag = MXMLDataUtils.getOffsetMXMLTag(mxmlData, currentOffset);
              if(offsetTag != null)
              {
                  //workaround for bug in Royale compiler
                  Position newPosition = new Position(position.getLine(), position.getCharacter() + 1);
                  int newOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), newPosition, includeFileData);
                  offsetNode = workspaceFolderManager.getEmbeddedActionScriptNodeInMXMLTag(offsetTag, path, newOffset, folderData);
              }
          }
      }
      IIdentifierNode identifierNode = null;
      if (offsetNode instanceof IIdentifierNode)
      {
          IASNode parentNode = offsetNode.getParent();
          if (parentNode instanceof IMemberAccessExpressionNode)
          {
              IMemberAccessExpressionNode memberAccessExpressionNode = (IMemberAccessExpressionNode) offsetNode.getParent();
              IExpressionNode leftOperandNode = memberAccessExpressionNode.getLeftOperandNode();
              if (leftOperandNode instanceof ILanguageIdentifierNode)
              {
                  ILanguageIdentifierNode leftIdentifierNode = (ILanguageIdentifierNode) leftOperandNode;
                  if (leftIdentifierNode.getKind() == ILanguageIdentifierNode.LanguageIdentifierKind.THIS)
                  {
                      identifierNode = (IIdentifierNode) offsetNode;
                  }
              }
          }
          else //no member access
          {
              identifierNode = (IIdentifierNode) offsetNode;
          }
      }
      if (identifierNode == null)
      {
          return;
      }
      String fileText = fileTracker.getText(path);
      if(fileText == null)
      {
          return;
      }
      WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForGenerateFieldVariable(
          identifierNode, path.toUri().toString(), fileText);
      if(edit == null)
      {
          return;
      }
      
      CodeAction codeAction = new CodeAction();
      codeAction.setDiagnostics(Collections.singletonList(diagnostic));
      codeAction.setTitle("Generate Field Variable");
      codeAction.setEdit(edit);
      codeAction.setKind(CodeActionKind.QuickFix);
      codeActions.add(Either.forRight(codeAction));
  }
 
Example 12
Source File: CodeActionProvider.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
private void createCodeActionForMissingLocalVariable(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions)
  {
      Position position = diagnostic.getRange().getStart();
      IncludeFileData includeFileData = folderData.includedFiles.get(path.toString());
int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData);
      IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData);
      if (offsetNode instanceof IMXMLInstanceNode)
      {
          MXMLData mxmlData = workspaceFolderManager.getMXMLDataForPath(path, folderData);
          if (mxmlData != null)
          {
              IMXMLTagData offsetTag = MXMLDataUtils.getOffsetMXMLTag(mxmlData, currentOffset);
              if(offsetTag != null)
              {
                  //workaround for bug in Royale compiler
                  Position newPosition = new Position(position.getLine(), position.getCharacter() + 1);
                  int newOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), newPosition, includeFileData);
                  offsetNode = workspaceFolderManager.getEmbeddedActionScriptNodeInMXMLTag(offsetTag, path, newOffset, folderData);
              }
          }
      }
      IIdentifierNode identifierNode = null;
      if (offsetNode instanceof IIdentifierNode)
      {
          identifierNode = (IIdentifierNode) offsetNode;
      }
      if (identifierNode == null)
      {
          return;
      }
      String fileText = fileTracker.getText(path);
      if(fileText == null)
      {
          return;
      }

      WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForGenerateLocalVariable(
          identifierNode, path.toUri().toString(), fileText);
      if(edit == null)
      {
          return;
      }

      CodeAction codeAction = new CodeAction();
      codeAction.setDiagnostics(Collections.singletonList(diagnostic));
      codeAction.setTitle("Generate Local Variable");
      codeAction.setEdit(edit);
      codeAction.setKind(CodeActionKind.QuickFix);
      codeActions.add(Either.forRight(codeAction));
  }
 
Example 13
Source File: CodeActionProvider.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
private void createCodeActionForUnimplementedMethods(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions)
  {
      ILspProject project = folderData.project;
      Position position = diagnostic.getRange().getStart();
      IncludeFileData includeFileData = folderData.includedFiles.get(path.toString());
int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData);
      IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData);
      if (offsetNode == null)
      {
          return;
      }

      IClassNode classNode = (IClassNode) offsetNode.getAncestorOfType(IClassNode.class);
      if (classNode == null)
      {
          return;
      }

      String fileText = fileTracker.getText(path);
      if(fileText == null)
      {
          return;
      }

      for (IExpressionNode exprNode : classNode.getImplementedInterfaceNodes())
      {
          IInterfaceDefinition interfaceDefinition = (IInterfaceDefinition) exprNode.resolve(project);
          if (interfaceDefinition == null)
          {
              continue;
          }
          WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForImplementInterface(
              classNode, interfaceDefinition, path.toUri().toString(), fileText, project);
          if (edit == null)
          {
              continue;
          }

          CodeAction codeAction = new CodeAction();
          codeAction.setDiagnostics(Collections.singletonList(diagnostic));
          codeAction.setTitle("Implement interface '" + interfaceDefinition.getBaseName() + "'");
          codeAction.setEdit(edit);
          codeAction.setKind(CodeActionKind.QuickFix);
          codeActions.add(Either.forRight(codeAction));
      }
  }
 
Example 14
Source File: CodeActionProvider.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
private void createCodeActionForMissingMethod(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions)
  {
      ILspProject project = folderData.project;
      Position position = diagnostic.getRange().getStart();
      IncludeFileData includeFileData = folderData.includedFiles.get(path.toString());
int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData);
      IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData);
      if (offsetNode == null)
      {
          return;
      }
      if (offsetNode instanceof IMXMLInstanceNode)
      {
          MXMLData mxmlData = workspaceFolderManager.getMXMLDataForPath(path, folderData);
          if (mxmlData != null)
          {
              IMXMLTagData offsetTag = MXMLDataUtils.getOffsetMXMLTag(mxmlData, currentOffset);
              if(offsetTag != null)
              {
                  //workaround for bug in Royale compiler
                  Position newPosition = new Position(position.getLine(), position.getCharacter() + 1);
                  int newOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), newPosition, includeFileData);
                  offsetNode = workspaceFolderManager.getEmbeddedActionScriptNodeInMXMLTag(offsetTag, path, newOffset, folderData);
              }
          }
      }
      IASNode parentNode = offsetNode.getParent();

      IFunctionCallNode functionCallNode = null;
      if (offsetNode instanceof IFunctionCallNode)
      {
          functionCallNode = (IFunctionCallNode) offsetNode;
      }
      else if (parentNode instanceof IFunctionCallNode)
      {
          functionCallNode = (IFunctionCallNode) offsetNode.getParent();
      }
      else if(offsetNode instanceof IIdentifierNode
              && parentNode instanceof IMemberAccessExpressionNode)
      {
          IASNode gpNode = parentNode.getParent();
          if (gpNode instanceof IFunctionCallNode)
          {
              functionCallNode = (IFunctionCallNode) gpNode;
          }
      }
      if (functionCallNode == null)
      {
          return;
      }
      String fileText = fileTracker.getText(path);
      if(fileText == null)
      {
          return;
      }

      WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForGenerateMethod(
          functionCallNode, path.toUri().toString(), fileText, project);
      if(edit == null)
      {
          return;
      }

      CodeAction codeAction = new CodeAction();
      codeAction.setDiagnostics(Collections.singletonList(diagnostic));
      codeAction.setTitle("Generate Method");
      codeAction.setEdit(edit);
      codeAction.setKind(CodeActionKind.QuickFix);
      codeActions.add(Either.forRight(codeAction));
  }
 
Example 15
Source File: CodeActionProvider.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
private void createCodeActionsForImport(Path path, Diagnostic diagnostic, WorkspaceFolderData folderData, List<Either<Command, CodeAction>> codeActions)
  {
      ILspProject project = folderData.project;
      Position position = diagnostic.getRange().getStart();
      IncludeFileData includeFileData = folderData.includedFiles.get(path.toString());
int currentOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), position, includeFileData);
      IASNode offsetNode = workspaceFolderManager.getOffsetNode(path, currentOffset, folderData);
      IMXMLTagData offsetTag = null;
      boolean isMXML = path.toUri().toString().endsWith(FILE_EXTENSION_MXML);
      if (isMXML)
      {
          MXMLData mxmlData = workspaceFolderManager.getMXMLDataForPath(path, folderData);
          if (mxmlData != null)
          {
              offsetTag = MXMLDataUtils.getOffsetMXMLTag(mxmlData, currentOffset);
          }
      }
      if (offsetNode instanceof IMXMLInstanceNode && offsetTag != null)
      {
          //workaround for bug in Royale compiler
          Position newPosition = new Position(position.getLine(), position.getCharacter() + 1);
          int newOffset = LanguageServerCompilerUtils.getOffsetFromPosition(fileTracker.getReader(path), newPosition, includeFileData);
          offsetNode = workspaceFolderManager.getEmbeddedActionScriptNodeInMXMLTag(offsetTag, path, newOffset, folderData);
      }
      if (offsetNode == null || !(offsetNode instanceof IIdentifierNode))
      {
          return;
      }
      ImportRange importRange = null;
      if (offsetTag != null)
      {
          importRange = ImportRange.fromOffsetTag(offsetTag, currentOffset);
      }
      else
      {
          importRange = ImportRange.fromOffsetNode(offsetNode);
      }
      String uri = importRange.uri;
      String fileText = fileTracker.getText(path);
      if(fileText == null)
      {
          return;
      }

      IIdentifierNode identifierNode = (IIdentifierNode) offsetNode;
      String typeString = identifierNode.getName();

      List<IDefinition> types = ASTUtils.findTypesThatMatchName(typeString, project.getCompilationUnits());
      for (IDefinition definitionToImport : types)
      {
          WorkspaceEdit edit = CodeActionsUtils.createWorkspaceEditForAddImport(definitionToImport, fileText, uri, importRange);
          if (edit == null)
          {
              continue;
          }
          CodeAction codeAction = new CodeAction();
          codeAction.setTitle("Import " + definitionToImport.getQualifiedName());
          codeAction.setEdit(edit);
          codeAction.setKind(CodeActionKind.QuickFix);
          codeAction.setDiagnostics(Collections.singletonList(diagnostic));
          codeActions.add(Either.forRight(codeAction));
      }
  }