Java Code Examples for org.eclipse.lsp4j.Position#setLine()

The following examples show how to use org.eclipse.lsp4j.Position#setLine() . 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: BaseDiagnosticsHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@SuppressWarnings("restriction")
private static Range convertRange(IOpenable openable, IProblem problem) {
	try {
		return JDTUtils.toRange(openable, problem.getSourceStart(), problem.getSourceEnd() - problem.getSourceStart() + 1);
	} catch (CoreException e) {
		// In case failed to open the IOpenable's buffer, use the IProblem's information to calculate the range.
		Position start = new Position();
		Position end = new Position();

		start.setLine(problem.getSourceLineNumber() - 1);// The protocol is 0-based.
		end.setLine(problem.getSourceLineNumber() - 1);
		if (problem instanceof DefaultProblem) {
			DefaultProblem dProblem = (DefaultProblem) problem;
			start.setCharacter(dProblem.getSourceColumnNumber() - 1);
			int offset = 0;
			if (dProblem.getSourceStart() != -1 && dProblem.getSourceEnd() != -1) {
				offset = dProblem.getSourceEnd() - dProblem.getSourceStart() + 1;
			}
			end.setCharacter(dProblem.getSourceColumnNumber() - 1 + offset);
		}
		return new Range(start, end);
	}
}
 
Example 2
Source File: XMLFormatter.java    From lemminx with Eclipse Public License 2.0 5 votes vote down vote up
private void enlargePositionToGutters(Position start, Position end) throws BadLocationException {
	start.setCharacter(0);

	if (end.getCharacter() == 0 && end.getLine() > 0) {
		end.setLine(end.getLine() - 1);
	}

	end.setCharacter(this.textDocument.lineText(end.getLine()).length());
}
 
Example 3
Source File: FormatterHandler.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static Position createPosition(IDocument document, int offset) {
	Position start = new Position();
	try {
		int lineOfOffset = document.getLineOfOffset(offset);
		start.setLine(Integer.valueOf(lineOfOffset));
		start.setCharacter(Integer.valueOf(offset - document.getLineOffset(lineOfOffset)));
	} catch (BadLocationException e) {
		JavaLanguageServerPlugin.logException(e.getMessage(), e);
	}
	return start;
}
 
Example 4
Source File: LanguageServerCompilerUtils.java    From vscode-as3mxml with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a compiler source location to a language server range. May
 * return null if the line or column of the source location is -1.
 */
public static Range getRangeFromSourceLocation(ISourceLocation sourceLocation)
{
    int line = sourceLocation.getLine();
    int column = sourceLocation.getColumn();
    if (line == -1 || column == -1)
    {
        //this is probably generated by the compiler somehow
        return null;
    }
    Position start = new Position();
    start.setLine(line);
    start.setCharacter(column);

    int endLine = sourceLocation.getEndLine();
    int endColumn = sourceLocation.getEndColumn();
    if (endLine == -1 || endColumn == -1)
    {
        endLine = line;
        endColumn = column;
    }
    Position end = new Position();
    end.setLine(endLine);
    end.setCharacter(endColumn);

    Range range = new Range();
    range.setStart(start);
    range.setEnd(end);

    return range;
}
 
Example 5
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 6
Source File: DefinitionTextUtils.java    From vscode-as3mxml with Apache License 2.0 5 votes vote down vote up
public Range toRange()
{
    Position start = new Position();
    start.setLine(startLine);
    start.setCharacter(startColumn);
    Position end = new Position();
    end.setLine(endLine);
    end.setCharacter(endColumn);
    Range range = new Range();
    range.setStart(start);
    range.setEnd(end);
    return range;
}
 
Example 7
Source File: DocumentTest.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private Position position(final int l, final int c) {
  Position _position = new Position();
  final Procedure1<Position> _function = (Position it) -> {
    it.setLine(l);
    it.setCharacter(c);
  };
  return ObjectExtensions.<Position>operator_doubleArrow(_position, _function);
}
 
Example 8
Source File: LanguageServerCompilerUtils.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
public static Position getPositionFromOffset(Reader in, int targetOffset, Position result)
{
    try
    {
        int offset = 0;
        int line = 0;
        int character = 0;

        while (offset < targetOffset)
        {
            int next = in.read();

            if (next < 0)
            {
                result.setLine(line);
                result.setCharacter(line);
                return result;
            }
            else
            {
                offset++;
                character++;

                if (next == '\n')
                {
                    line++;
                    character = 0;
                }
            }
        }

        result.setLine(line);
        result.setCharacter(character);
    }
    catch (IOException e)
    {
        result.setLine(-1);
        result.setCharacter(-1);
    }
    return result;
}
 
Example 9
Source File: CodeActionsUtils.java    From vscode-as3mxml with Apache License 2.0 4 votes vote down vote up
public static AddImportData findAddImportData(String fileText, ImportRange importRange)
{
    int startIndex = importRange.startIndex;
    int endIndex = importRange.endIndex;
    if(startIndex == -1)
    {
        startIndex = 0;
    }
    int textLength = fileText.length();
    if (endIndex == -1 || endIndex > textLength)
    {
        //it's possible for the end index to be longer than the text
        //for example, if the package block is incomplete in an .as file
        endIndex = textLength;
    }
    String indent = "";
    String lineBreaks = "\n";
    int importIndex = -1;
    Matcher importMatcher = importPattern.matcher(fileText);
    importMatcher.region(startIndex, endIndex);
    while (importMatcher.find())
    {
        indent = importMatcher.group(1);
        importIndex = importMatcher.start();
    }
    Position position = null;
    if(importIndex != -1) //found existing imports
    {
        position = LanguageServerCompilerUtils.getPositionFromOffset(new StringReader(fileText), importIndex);
        position.setLine(position.getLine() + 1);
        position.setCharacter(0);
    }
    else //no existing imports
    {
        if(importRange.needsMXMLScript)
        {
            position = LanguageServerCompilerUtils.getPositionFromOffset(new StringReader(fileText), importRange.endIndex);
        }
        else
        {
            //start by looking for the package block
            Matcher packageMatcher = packagePattern.matcher(fileText);
            packageMatcher.region(startIndex, endIndex);
            if (packageMatcher.find()) //found the package
            {
                position = LanguageServerCompilerUtils.getPositionFromOffset(
                    new StringReader(fileText), packageMatcher.end());
                if(position.getCharacter() > 0)
                {
                    //go to the beginning of the line, if we're not there
                    position.setCharacter(0);
                }
                indent = packageMatcher.group(1);
            }
            else //couldn't find the start of a package or existing imports
            {
                position = LanguageServerCompilerUtils.getPositionFromOffset(new StringReader(fileText), startIndex);
                if (position.getCharacter() > 0)
                {
                    //go to the next line, if we're not at the start
                    position.setLine(position.getLine() + 1);
                    position.setCharacter(0);
                }
                //try to use the same indent as whatever follows
                Matcher indentMatcher = indentPattern.matcher(fileText);
                indentMatcher.region(startIndex, endIndex);
                if (indentMatcher.find())
                {
                    indent = indentMatcher.group(1);
                }
            }
        }
        lineBreaks += "\n"; //add an extra line break
    }
    return new AddImportData(position, indent, lineBreaks, importRange);
}