forked from phoedos/pmd
Add method for designer
This commit is contained in:
@@ -121,6 +121,23 @@ public interface Node extends Reportable {
|
||||
@Override
|
||||
FileLocation getReportLocation();
|
||||
|
||||
/**
|
||||
* Returns a region of text delimiting the node in the underlying
|
||||
* text document. This does not necessarily match the
|
||||
* {@link #getReportLocation() report location}.
|
||||
*
|
||||
* @implNote The default implementation uses the {@link #getReportLocation()}
|
||||
* to create a region.
|
||||
*/
|
||||
default TextRegion getTextRegion() {
|
||||
@SuppressWarnings("PMD.CloseResource")
|
||||
TextDocument document = getAstInfo().getTextDocument();
|
||||
FileLocation loc = getReportLocation();
|
||||
int startOffset = document.offsetAtLineColumn(loc.getStartLine(), loc.getStartColumn());
|
||||
int endOffset = document.offsetAtLineColumn(loc.getEndLine(), loc.getEndColumn());
|
||||
return TextRegion.fromBothOffsets(startOffset, endOffset);
|
||||
}
|
||||
|
||||
|
||||
// Those are kept here because they're handled specially as XPath
|
||||
// attributes
|
||||
|
@@ -20,8 +20,11 @@ public interface TextAvailableNode extends Node {
|
||||
* Returns the exact region of text delimiting
|
||||
* the node in the underlying text document. Note
|
||||
* that {@link #getReportLocation()} does not need
|
||||
* to match this region.
|
||||
* to match this region. {@link #getReportLocation()}
|
||||
* can be scoped down to a specific token, eg the
|
||||
* class identifier.
|
||||
*/
|
||||
@Override
|
||||
TextRegion getTextRegion();
|
||||
|
||||
/**
|
||||
|
@@ -77,8 +77,18 @@ public final class FileLocation {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inclusive, 1-based line number.
|
||||
*
|
||||
* @deprecated Use {@link #getStartLine()}.
|
||||
*/
|
||||
@Deprecated
|
||||
public int getBeginLine() { // todo rename to getStartLine
|
||||
return getStartLine();
|
||||
}
|
||||
|
||||
/** Inclusive, 1-based line number. */
|
||||
public int getBeginLine() {
|
||||
public int getStartLine() {
|
||||
return beginLine;
|
||||
}
|
||||
|
||||
@@ -87,8 +97,18 @@ public final class FileLocation {
|
||||
return endLine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inclusive, 1-based column number.
|
||||
*
|
||||
* @deprecated Use {@link #getStartColumn()}.
|
||||
*/
|
||||
@Deprecated
|
||||
public int getBeginColumn() { // todo rename to getStartLine
|
||||
return getStartColumn();
|
||||
}
|
||||
|
||||
/** Inclusive, 1-based column number. */
|
||||
public int getBeginColumn() {
|
||||
public int getStartColumn() {
|
||||
return beginColumn;
|
||||
}
|
||||
|
||||
|
@@ -83,6 +83,12 @@ final class RootTextDocument extends BaseCloseable implements TextDocument {
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int offsetAtLineColumn(int line, int column) {
|
||||
SourceCodePositioner positioner = content.getPositioner();
|
||||
return positioner.offsetFromLineColumn(line, column);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextRegion createLineRange(int startLineInclusive, int endLineInclusive) {
|
||||
SourceCodePositioner positioner = content.getPositioner();
|
||||
|
@@ -111,7 +111,9 @@ public interface TextDocument extends Closeable {
|
||||
|
||||
|
||||
/**
|
||||
* Turn a text region into a {@link FileLocation}.
|
||||
* Turn a text region into a {@link FileLocation}. This computes
|
||||
* the line/column information for both start and end offset of
|
||||
* the region.
|
||||
*
|
||||
* @return A new file position
|
||||
*
|
||||
@@ -119,8 +121,15 @@ public interface TextDocument extends Closeable {
|
||||
*/
|
||||
FileLocation toLocation(TextRegion region);
|
||||
|
||||
|
||||
// todo doc
|
||||
/**
|
||||
* Create a location from its positions as lines/columns. The file
|
||||
* name is the display name of this document.
|
||||
*
|
||||
* @param bline Start line
|
||||
* @param bcol Start column
|
||||
* @param eline End line
|
||||
* @param ecol End column
|
||||
*/
|
||||
default FileLocation createLocation(int bline, int bcol, int eline, int ecol) {
|
||||
return FileLocation.location(getDisplayName(), bline, bcol, eline, ecol);
|
||||
}
|
||||
@@ -136,6 +145,17 @@ public interface TextDocument extends Closeable {
|
||||
return toLocation(TextRegion.fromOffsetLength(offset, 0)).getBeginLine();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the offset at the given line and column number. This is
|
||||
* the dual of
|
||||
*
|
||||
* @param line Line number (1-based)
|
||||
* @param column Column number (1-based)
|
||||
*
|
||||
* @return an offset (0-based)
|
||||
*/
|
||||
int offsetAtLineColumn(int line, int column);
|
||||
|
||||
/**
|
||||
* Closing a document closes the underlying {@link TextFile}.
|
||||
* New editors cannot be produced after that, and the document otherwise
|
||||
|
@@ -194,6 +194,17 @@ public final class TextRegion implements Comparable<TextRegion> {
|
||||
return new TextRegion(startOffset, endOffset - startOffset);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a new region with zero length and placed at the given offset.
|
||||
*
|
||||
* @param startOffset Offset for start and end of the position.
|
||||
*
|
||||
* @throws AssertionError If the offset is negative
|
||||
*/
|
||||
public static TextRegion caretAt(int startOffset) {
|
||||
return new TextRegion(startOffset, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the parameters are a valid region, this is provided
|
||||
* to debug, will be a noop unless assertions are enabled.
|
||||
|
@@ -140,6 +140,26 @@ public class TextDocumentTest {
|
||||
assertEquals(1 + "bonjour".length(), withLines.getEndColumn());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOffsetFromLineColumn() {
|
||||
TextDocument doc = TextDocument.readOnlyString("bonjour\noa\n", dummyVersion);
|
||||
|
||||
assertEquals(0, doc.offsetAtLineColumn(1, 1));
|
||||
assertEquals(1, doc.offsetAtLineColumn(1, 2));
|
||||
assertEquals(2, doc.offsetAtLineColumn(1, 3));
|
||||
assertEquals(3, doc.offsetAtLineColumn(1, 4));
|
||||
assertEquals(4, doc.offsetAtLineColumn(1, 5));
|
||||
assertEquals(5, doc.offsetAtLineColumn(1, 6));
|
||||
assertEquals(6, doc.offsetAtLineColumn(1, 7));
|
||||
assertEquals(7, doc.offsetAtLineColumn(1, 8));
|
||||
assertEquals(8, doc.offsetAtLineColumn(1, 9));
|
||||
assertEquals(8, doc.offsetAtLineColumn(2, 1));
|
||||
assertEquals(9, doc.offsetAtLineColumn(2, 2));
|
||||
assertEquals(10, doc.offsetAtLineColumn(2, 3));
|
||||
assertEquals(11, doc.offsetAtLineColumn(2, 4));
|
||||
assertEquals(11, doc.offsetAtLineColumn(3, 1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLineRange() {
|
||||
TextDocument doc = TextDocument.readOnlyString("bonjour\noha\ntristesse", dummyVersion);
|
||||
|
@@ -39,7 +39,8 @@ abstract class AbstractEcmascriptNode<T extends AstNode> extends AbstractNode<Ab
|
||||
return getTextDocument().toLocation(getTextRegion());
|
||||
}
|
||||
|
||||
private TextRegion getTextRegion() {
|
||||
@Override
|
||||
public TextRegion getTextRegion() {
|
||||
return TextRegion.fromOffsetLength(node.getAbsolutePosition(), node.getLength());
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user