Java Code Examples for org.eclipse.lsp4j.TextEdit#setNewText()

The following examples show how to use org.eclipse.lsp4j.TextEdit#setNewText() . 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: RenameProvider.java    From groovy-language-server with Apache License 2.0 6 votes vote down vote up
private TextEdit createTextEditToRenameMethodNode(MethodNode methodNode, String newName, String text, Range range) {
	// the AST doesn't give us access to the name location, so we
	// need to find it manually
	Pattern methodPattern = Pattern.compile("\\b" + methodNode.getName() + "\\b(?=\\s*\\()");
	Matcher methodMatcher = methodPattern.matcher(text);
	if (!methodMatcher.find()) {
		// couldn't find the name!
		return null;
	}

	Position start = range.getStart();
	Position end = range.getEnd();
	end.setCharacter(start.getCharacter() + methodMatcher.end());
	start.setCharacter(start.getCharacter() + methodMatcher.start());

	TextEdit textEdit = new TextEdit();
	textEdit.setRange(range);
	textEdit.setNewText(newName);
	return textEdit;
}
 
Example 2
Source File: RenameProvider.java    From groovy-language-server with Apache License 2.0 6 votes vote down vote up
private TextEdit createTextEditToRenamePropertyNode(PropertyNode propNode, String newName, String text,
		Range range) {
	// the AST doesn't give us access to the name location, so we
	// need to find it manually
	Pattern propPattern = Pattern.compile("\\b" + propNode.getName() + "\\b");
	Matcher propMatcher = propPattern.matcher(text);
	if (!propMatcher.find()) {
		// couldn't find the name!
		return null;
	}

	Position start = range.getStart();
	Position end = range.getEnd();
	end.setCharacter(start.getCharacter() + propMatcher.end());
	start.setCharacter(start.getCharacter() + propMatcher.start());

	TextEdit textEdit = new TextEdit();
	textEdit.setRange(range);
	textEdit.setNewText(newName);
	return textEdit;
}
 
Example 3
Source File: DocumentTest.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
private TextEdit textEdit(final Position startPos, final Position endPos, final String newText) {
  TextEdit _textEdit = new TextEdit();
  final Procedure1<TextEdit> _function = (TextEdit it) -> {
    if ((startPos != null)) {
      Range _range = new Range();
      final Procedure1<Range> _function_1 = (Range it_1) -> {
        it_1.setStart(startPos);
        it_1.setEnd(endPos);
      };
      Range _doubleArrow = ObjectExtensions.<Range>operator_doubleArrow(_range, _function_1);
      it.setRange(_doubleArrow);
    }
    it.setNewText(newText);
  };
  return ObjectExtensions.<TextEdit>operator_doubleArrow(_textEdit, _function);
}
 
Example 4
Source File: RenameProvider.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
private TextEdit createTextEditToRenameClassNode(ClassNode classNode, String newName, String text, Range range) {
	// the AST doesn't give us access to the name location, so we
	// need to find it manually
	String className = classNode.getNameWithoutPackage();
	int dollarIndex = className.indexOf('$');
	if (dollarIndex != 01) {
		// it's an inner class, so remove the outer name prefix
		className = className.substring(dollarIndex + 1);
	}

	Pattern classPattern = Pattern.compile("(class\\s+)" + className + "\\b");
	Matcher classMatcher = classPattern.matcher(text);
	if (!classMatcher.find()) {
		// couldn't find the name!
		return null;
	}
	String prefix = classMatcher.group(1);

	Position start = range.getStart();
	Position end = range.getEnd();
	end.setCharacter(start.getCharacter() + classMatcher.end());
	start.setCharacter(start.getCharacter() + prefix.length() + classMatcher.start());

	TextEdit textEdit = new TextEdit();
	textEdit.setRange(range);
	textEdit.setNewText(newName);
	return textEdit;
}
 
Example 5
Source File: CompletionProvider.java    From groovy-language-server with Apache License 2.0 5 votes vote down vote up
private TextEdit createAddImportTextEdit(String className, Range range) {
	TextEdit edit = new TextEdit();
	StringBuilder builder = new StringBuilder();
	builder.append("import ");
	builder.append(className);
	builder.append("\n");
	edit.setNewText(builder.toString());
	edit.setRange(range);
	return edit;
}
 
Example 6
Source File: CodeActionsUtils.java    From vscode-as3mxml with Apache License 2.0 5 votes vote down vote up
public static TextEdit createTextEditForAddMXMLNamespace(String nsPrefix, String nsURI, Position position)
{
    String textToInsert = " xmlns:" + nsPrefix + "=\"" + nsURI + "\"";

    TextEdit edit = new TextEdit();
    edit.setNewText(textToInsert);
    edit.setRange(new Range(position, position));
    return edit;
}
 
Example 7
Source File: CodeActionsUtils.java    From vscode-as3mxml with Apache License 2.0 5 votes vote down vote up
public static TextEdit createTextEditForGenerateLocalVariable(
    IIdentifierNode identifierNode, String text)
{
    IFunctionNode functionNode = (IFunctionNode) identifierNode.getAncestorOfType(IFunctionNode.class);
    if (functionNode == null)
    {
        return null;
    }
    IScopedNode scopedNode = functionNode.getScopedNode();
    if (scopedNode == null || scopedNode.getChildCount() == 0)
    {
        return null;
    }

    IASNode firstChild = scopedNode.getChild(0);

    String indent = ASTUtils.getIndentBeforeNode(firstChild, text);

    StringBuilder builder = new StringBuilder();
    builder.append(indent);
    builder.append(IASKeywordConstants.VAR);
    builder.append(" ");
    builder.append(identifierNode.getName());
    builder.append(":");
    builder.append(IASLanguageConstants.Object);
    builder.append(";");
    builder.append(NEW_LINE);

    TextEdit textEdit = new TextEdit();
    textEdit.setNewText(builder.toString());
    Position editPosition = new Position(scopedNode.getLine() + 1, 0);
    textEdit.setRange(new Range(editPosition, editPosition));
    return textEdit;
}
 
Example 8
Source File: CodeActionsUtils.java    From vscode-as3mxml with Apache License 2.0 5 votes vote down vote up
public static TextEdit createTextEditForGenerateFieldVariable(
    IIdentifierNode identifierNode, String text)
{
    LineAndIndent lineAndIndent = findLineAndIndent(identifierNode, text);
    if(lineAndIndent == null)
    {
        return null;
    }

    StringBuilder builder = new StringBuilder();
    builder.append(NEW_LINE);
    builder.append(lineAndIndent.indent);
    builder.append(IASKeywordConstants.PUBLIC);
    builder.append(" ");
    builder.append(IASKeywordConstants.VAR);
    builder.append(" ");
    builder.append(identifierNode.getName());
    builder.append(":");
    builder.append(IASLanguageConstants.Object);
    builder.append(";");
    builder.append(NEW_LINE);

    TextEdit textEdit = new TextEdit();
    textEdit.setNewText(builder.toString());
    Position editPosition = new Position(lineAndIndent.line, 0);
    textEdit.setRange(new Range(editPosition, editPosition));
    return textEdit;
}
 
Example 9
Source File: CodeActionsUtils.java    From vscode-as3mxml with Apache License 2.0 5 votes vote down vote up
public static TextEdit createTextEditForRemoveUnusedImport(String text, Range range)
{
    int startLine = range.getStart().getLine();
    int endLine = range.getEnd().getLine();
    int endChar = range.getEnd().getCharacter();

    Position startPosition = new Position(startLine, 0);
    Position endPosition = new Position(endLine, endChar);

    Range resultRange = new Range(startPosition, endPosition);

    //we may need to adjust the end position to include the semi-colon and new line
    int offset = LanguageServerCompilerUtils.getOffsetFromPosition(new StringReader(text), endPosition);
    if (offset < text.length() && text.charAt(offset) == ';')
    {
        endPosition.setCharacter(endPosition.getCharacter() + 1);
        offset++;
    }
    if (offset < text.length() && (text.charAt(offset) == '\r' || text.charAt(offset) == '\n'))
    {
        endPosition.setLine(endPosition.getLine() + 1);
        endPosition.setCharacter(0);
    }
    
    TextEdit textEdit = new TextEdit();
    textEdit.setNewText("");
    textEdit.setRange(resultRange);
    return textEdit;
}
 
Example 10
Source File: CodeActionService.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private Command fixInvalidName(Diagnostic d, ICodeActionService2.Options options) {
	String string = options.getDocument().getSubstring(d.getRange());
	Command command = new Command();
	command.setCommand("my.textedit.command");
	command.setTitle("Make '" + string + "' upper case");
	WorkspaceEdit workspaceEdit = new WorkspaceEdit();
	TextEdit textEdit = new TextEdit();
	textEdit.setNewText(StringExtensions.toFirstUpper(string));
	textEdit.setRange(d.getRange());
	workspaceEdit.getChanges().put(options.getCodeActionParams().getTextDocument().getUri(),
			Lists.newArrayList(textEdit));
	command.setArguments(Lists.newArrayList(workspaceEdit));
	return command;
}
 
Example 11
Source File: XMLAssert.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
public static TextEdit te(int startLine, int startCharacter, int endLine, int endCharacter, String newText) {
	TextEdit textEdit = new TextEdit();
	textEdit.setNewText(newText);
	textEdit.setRange(r(startLine, startCharacter, endLine, endCharacter));
	return textEdit;
}
 
Example 12
Source File: CodeActionsUtils.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
public static TextEdit createTextEditForAddImports(List<String> qualifiedNames, AddImportData addImportData)
{
    if(qualifiedNames.size() == 0)
    {
        return null;
    }

    Position position = addImportData.position;
    String indent = addImportData.indent;
    String newLines = addImportData.newLines;

    StringBuilder builder = new StringBuilder();

    if(addImportData.importRange.needsMXMLScript)
    {
        builder.append("\t");
        builder.append("<");
        builder.append(addImportData.importRange.mxmlLanguageNS.prefix);
        builder.append(":");
        builder.append("Script");
        builder.append(">");
        builder.append("\n\t\t");
        builder.append("<![CDATA[");
        builder.append("\n");
    }

    for(int i = 0; i < qualifiedNames.size(); i++)
    {
        String qualifiedName = qualifiedNames.get(i);
        if(addImportData.importRange.needsMXMLScript)
        {
            builder.append("\t\t\t");
        }
        else
        {
            builder.append(indent);
        }
        builder.append(IASKeywordConstants.IMPORT);
        builder.append(" ");
        builder.append(qualifiedName);
        builder.append(";");
        if(i < (qualifiedNames.size() - 1))
        {
            builder.append("\n");
        }
    }
    builder.append(newLines);

    if(addImportData.importRange.needsMXMLScript)
    {
        builder.append("\t\t");
        builder.append("]]>");
        builder.append("\n\t");
        builder.append("</");
        builder.append(addImportData.importRange.mxmlLanguageNS.prefix);
        builder.append(":");
        builder.append("Script");
        builder.append(">");
        builder.append("\n");
    }
    
    TextEdit textEdit = new TextEdit();
    textEdit.setNewText(builder.toString());
    textEdit.setRange(new Range(position, position));
    return textEdit;
}
 
Example 13
Source File: CodeActionsUtils.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
private static TextEdit createTextEditForImplementMethod(
    IClassNode classNode, IFunctionDefinition functionDefinition, String text, ICompilerProject project)
{
    int line = 0;
    String indent = "";
    IScopedNode scopedNode = classNode.getScopedNode();
    if (scopedNode == null)
    {
        return null;
    }
    line = scopedNode.getEndLine();

    if(scopedNode.getChildCount() > 0)
    {
        indent = ASTUtils.getIndentBeforeNode(scopedNode.getChild(scopedNode.getChildCount() - 1), text);
    }

    StringBuilder builder = new StringBuilder();
    builder.append(NEW_LINE);
    builder.append(indent);
    builder.append(IASKeywordConstants.PUBLIC);
    builder.append(" ");
    builder.append(IASKeywordConstants.FUNCTION);
    builder.append(" ");
    if(functionDefinition instanceof IGetterDefinition)
    {
        builder.append(IASKeywordConstants.GET);
        builder.append(" ");
    }
    if(functionDefinition instanceof ISetterDefinition)
    {
        builder.append(IASKeywordConstants.SET);
        builder.append(" ");
    }
    builder.append(DefinitionTextUtils.functionDefinitionToSignature(functionDefinition, project));
    builder.append(NEW_LINE);
    builder.append(indent);
    builder.append("{");
    builder.append(NEW_LINE);
    builder.append(indent);
    builder.append("\t");
    builder.append(IASKeywordConstants.THROW);
    builder.append(" ");
    builder.append(IASKeywordConstants.NEW);
    builder.append(" ");
    builder.append(IASLanguageConstants.Error);
    builder.append("(");
    builder.append("\"Method not implemented.\"");
    builder.append(")");
    builder.append(";");
    builder.append(NEW_LINE);
    builder.append(indent);
    builder.append("}");
    builder.append(NEW_LINE);

    TextEdit textEdit = new TextEdit();
    textEdit.setNewText(builder.toString());
    Position editPosition = new Position(line, 0);
    textEdit.setRange(new Range(editPosition, editPosition));

    return textEdit;
}
 
Example 14
Source File: CodeActionsUtils.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
private static TextEdit createTextEditForGenerateCatch(
    ITryNode tryNode, String text, ICompilerProject project)
{
    IASNode statementContentsNode = tryNode.getStatementContentsNode();
    if (statementContentsNode == null
            || !(statementContentsNode instanceof IBlockNode)
            || !(statementContentsNode instanceof IContainerNode))
    {
        return null;
    }
    IContainerNode containerNode = (IContainerNode) statementContentsNode;
    if (containerNode.getContainerType().equals(ContainerType.SYNTHESIZED))
    {
        //this should be fixed first
        return null;
    }
    if(tryNode.getCatchNodeCount() > 0)
    {
        return null;
    }

    TextEdit textEdit = new TextEdit();

    String indent = ASTUtils.getIndentBeforeNode(tryNode, text);

    StringBuilder builder = new StringBuilder();
    builder.append(NEW_LINE);
    builder.append(indent);
    builder.append(IASKeywordConstants.CATCH);
    builder.append("(");
    builder.append("e");
    builder.append(":");
    builder.append(IASLanguageConstants.Error);
    builder.append(")");
    builder.append(NEW_LINE);
    builder.append(indent);
    builder.append("{");
    builder.append(NEW_LINE);
    builder.append(indent);
    builder.append("}");

    textEdit.setNewText(builder.toString());
    
    int column = containerNode.getEndColumn();
    if (containerNode.getContainerType().equals(ContainerType.BRACES))
    {
        column++;
    }

    Position editPosition = new Position(containerNode.getEndLine(), column);
    textEdit.setRange(new Range(editPosition, editPosition));

    return textEdit;
}
 
Example 15
Source File: CodeActionsUtils.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
private static List<TextEdit> createTextEditsForGenerateEventListener(
    IASNode context, String functionName, String eventClassName,
    String text, ICompilerProject project)
{
    LineAndIndent lineAndIndent = findLineAndIndent(context, text);
    if(lineAndIndent == null)
    {
        return null;
    }

    List<TextEdit> edits = new ArrayList<>();
    TextEdit textEdit = new TextEdit();
    edits.add(textEdit);

    StringBuilder builder = new StringBuilder();
    builder.append(NEW_LINE);
    builder.append(lineAndIndent.indent);
    builder.append(IASKeywordConstants.PRIVATE);
    builder.append(" ");
    builder.append(IASKeywordConstants.FUNCTION);
    builder.append(" ");
    builder.append(functionName);
    builder.append("(");
    
    builder.append("event");
    builder.append(":");

    ImportRange importRange = ImportRange.fromOffsetNode(context);
    int index = eventClassName.lastIndexOf(".");
    if (index == -1)
    {
        builder.append(eventClassName);
    }
    else
    {
        builder.append(eventClassName.substring(index + 1));
    }
    if (ASTUtils.needsImport(context, eventClassName))
    {
        TextEdit importEdit = CodeActionsUtils.createTextEditForAddImport(eventClassName, text, importRange);
        if (importEdit != null)
        {
            edits.add(importEdit);
        }
    }
    builder.append(")");
    builder.append(":");
    builder.append(IASKeywordConstants.VOID);
    builder.append(NEW_LINE);
    builder.append(lineAndIndent.indent);
    builder.append("{");
    builder.append(NEW_LINE);
    builder.append(lineAndIndent.indent);
    builder.append("}");
    builder.append(NEW_LINE);

    textEdit.setNewText(builder.toString());
    Position editPosition = new Position(lineAndIndent.line, 0);
    textEdit.setRange(new Range(editPosition, editPosition));

    return edits;
}
 
Example 16
Source File: FormattingService.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
protected TextEdit toTextEdit(Document document, String formattedText, int startOffset, int length) {
	TextEdit textEdit = new TextEdit();
	textEdit.setNewText(formattedText);
	textEdit.setRange(new Range(document.getPosition(startOffset), document.getPosition((startOffset + length))));
	return textEdit;
}