Package org.eclipse.lemminx.commons
Class TreeLineTracker
- java.lang.Object
-
- org.eclipse.lemminx.commons.TreeLineTracker
-
- All Implemented Interfaces:
ILineTracker
public class TreeLineTracker extends Object implements ILineTracker
Abstract implementation ofILineTracker. It lets the definition of line delimiters to subclasses. Assuming that '\n' is the only line delimiter, this abstract implementation defines the following line scheme:- "" -> [0,0]
- "a" -> [0,1]
- "\n" -> [0,1], [1,0]
- "a\n" -> [0,2], [2,0]
- "a\nb" -> [0,2], [2,1]
- "a\nbc\n" -> [0,2], [2,3], [5,0]
This class must be subclassed.
Performance: The query operations perform in O(log n) where n is the number of lines in the document. The modification operations roughly perform in O(l * log n) where n is the number of lines in the document and l is the sum of the number of removed, added or modified lines.
- Since:
- 3.2
-
-
Field Summary
Fields Modifier and Type Field Description static String[]DELIMITERSThe predefined delimiters of this tracker
-
Constructor Summary
Constructors Modifier Constructor Description protectedTreeLineTracker()Creates a new line tracker.TreeLineTracker(org.eclipse.lemminx.commons.ListLineTracker tracker)Package visible constructor for creating a tree tracker from a list tracker.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intcomputeNumberOfLines(String text)Computes the number of lines in the given text.StringgetLineDelimiter(int line)Returns the line delimiter of the specified line.org.eclipse.lemminx.commons.LinegetLineInformation(int line)Returns a line description of the given line.org.eclipse.lemminx.commons.LinegetLineInformationOfOffset(int offset)Returns a line description of the line at the given offset.intgetLineLength(int line)Returns length of the specified line including the line's delimiter.intgetLineNumberOfOffset(int offset)Returns the line number the character at the given offset belongs to.intgetLineOffset(int line)Returns the position of the first character of the specified line.intgetNumberOfLines()Returns the number of lines.intgetNumberOfLines(int offset, int length)Returns the number of lines which are occupied by a given text range.intgetOffsetAt(org.eclipse.lsp4j.Position position)org.eclipse.lsp4j.PositiongetPositionAt(int offset)protected org.eclipse.lemminx.commons.ListLineTracker.DelimiterInfonextDelimiterInfo(String text, int offset)Returns the information about the first delimiter found in the given text starting at the given offset.voidreplace(int offset, int length, String text)Informs the line tracker about the specified change in the tracked text.voidset(String text)Sets the tracked text to the specified text.StringtoString()
-
-
-
Field Detail
-
DELIMITERS
public static final String[] DELIMITERS
The predefined delimiters of this tracker
-
-
Constructor Detail
-
TreeLineTracker
protected TreeLineTracker()
Creates a new line tracker.
-
TreeLineTracker
public TreeLineTracker(org.eclipse.lemminx.commons.ListLineTracker tracker)
Package visible constructor for creating a tree tracker from a list tracker.- Parameters:
tracker- the list line tracker
-
-
Method Detail
-
replace
public final void replace(int offset, int length, String text) throws BadLocationExceptionDescription copied from interface:ILineTrackerInforms the line tracker about the specified change in the tracked text.- Specified by:
replacein interfaceILineTracker- Parameters:
offset- the offset of the replaced textlength- the length of the replaced texttext- the substitution text- Throws:
BadLocationException- if specified range is unknown to this tracker
-
nextDelimiterInfo
protected org.eclipse.lemminx.commons.ListLineTracker.DelimiterInfo nextDelimiterInfo(String text, int offset)
Returns the information about the first delimiter found in the given text starting at the given offset.- Parameters:
text- the text to be searchedoffset- the offset in the given text- Returns:
- the information of the first found delimiter or
null
-
getLineDelimiter
public final String getLineDelimiter(int line) throws BadLocationException
Description copied from interface:ILineTrackerReturns the line delimiter of the specified line. Returnsnullif the line is not closed with a line delimiter.- Specified by:
getLineDelimiterin interfaceILineTracker- Parameters:
line- the line whose line delimiter is queried- Returns:
- the line's delimiter or
nullif line does not have a delimiter - Throws:
BadLocationException- if the line number is invalid in this tracker's line structure
-
computeNumberOfLines
public final int computeNumberOfLines(String text)
Description copied from interface:ILineTrackerComputes the number of lines in the given text.- Specified by:
computeNumberOfLinesin interfaceILineTracker- Parameters:
text- the text whose number of lines should be computed- Returns:
- the number of lines in the given text
-
getNumberOfLines
public final int getNumberOfLines()
Description copied from interface:ILineTrackerReturns the number of lines.Note that a document always has at least one line.
- Specified by:
getNumberOfLinesin interfaceILineTracker- Returns:
- the number of lines in this tracker's line structure
-
getNumberOfLines
public final int getNumberOfLines(int offset, int length) throws BadLocationExceptionDescription copied from interface:ILineTrackerReturns the number of lines which are occupied by a given text range.- Specified by:
getNumberOfLinesin interfaceILineTracker- Parameters:
offset- the offset of the specified text rangelength- the length of the specified text range- Returns:
- the number of lines occupied by the specified range
- Throws:
BadLocationException- if specified range is unknown to this tracker
-
getLineOffset
public final int getLineOffset(int line) throws BadLocationExceptionDescription copied from interface:ILineTrackerReturns the position of the first character of the specified line.- Specified by:
getLineOffsetin interfaceILineTracker- Parameters:
line- the line of interest- Returns:
- offset of the first character of the line
- Throws:
BadLocationException- if the line is unknown to this tracker
-
getLineLength
public final int getLineLength(int line) throws BadLocationExceptionDescription copied from interface:ILineTrackerReturns length of the specified line including the line's delimiter.- Specified by:
getLineLengthin interfaceILineTracker- Parameters:
line- the line of interest- Returns:
- the length of the line
- Throws:
BadLocationException- if line is unknown to this tracker
-
getLineNumberOfOffset
public final int getLineNumberOfOffset(int offset) throws BadLocationExceptionDescription copied from interface:ILineTrackerReturns the line number the character at the given offset belongs to.- Specified by:
getLineNumberOfOffsetin interfaceILineTracker- Parameters:
offset- the offset whose line number to be determined- Returns:
- the number of the line the offset is on
- Throws:
BadLocationException- if the offset is invalid in this tracker
-
getPositionAt
public final org.eclipse.lsp4j.Position getPositionAt(int offset) throws BadLocationException- Specified by:
getPositionAtin interfaceILineTracker- Throws:
BadLocationException
-
getOffsetAt
public int getOffsetAt(org.eclipse.lsp4j.Position position) throws BadLocationException- Specified by:
getOffsetAtin interfaceILineTracker- Throws:
BadLocationException
-
getLineInformationOfOffset
public final org.eclipse.lemminx.commons.Line getLineInformationOfOffset(int offset) throws BadLocationExceptionDescription copied from interface:ILineTrackerReturns a line description of the line at the given offset. The description contains the start offset and the length of the line excluding the line's delimiter.- Specified by:
getLineInformationOfOffsetin interfaceILineTracker- Parameters:
offset- the offset whose line should be described- Returns:
- a region describing the line
- Throws:
BadLocationException- if offset is invalid in this tracker
-
getLineInformation
public final org.eclipse.lemminx.commons.Line getLineInformation(int line) throws BadLocationExceptionDescription copied from interface:ILineTrackerReturns a line description of the given line. The description contains the start offset and the length of the line excluding the line's delimiter.- Specified by:
getLineInformationin interfaceILineTracker- Parameters:
line- the line that should be described- Returns:
- a region describing the line
- Throws:
BadLocationException- if line is unknown to this tracker
-
set
public final void set(String text)
Description copied from interface:ILineTrackerSets the tracked text to the specified text.- Specified by:
setin interfaceILineTracker- Parameters:
text- the new tracked text
-
-