forked from phoedos/pmd
Remove some things from TextDocument
This commit is contained in:
parent
935b7fc4e2
commit
d1d2f30119
@ -57,6 +57,8 @@ public final class JavaccTokenDocument extends TokenDocument<JavaccToken> {
|
||||
* @param text Source doc
|
||||
*
|
||||
* @see EscapeTranslator
|
||||
*
|
||||
* TODO move that to LanguageVersionHandler once #3919 (Merge CPD and PMD language) is implemented
|
||||
*/
|
||||
protected TextDocument translate(TextDocument text) throws MalformedSourceException {
|
||||
return text;
|
||||
|
@ -59,19 +59,9 @@ abstract class BaseMappedDocument implements TextDocument {
|
||||
return base.createLineRange(startLineInclusive, endLineInclusive);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int offsetAtLineColumn(int line, int column) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TextPos2d lineColumnAtOffset(int offset, boolean inclusive) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInRange(TextPos2d textPos2d) {
|
||||
return false;
|
||||
return base.lineColumnAtOffset(localOffsetTransform(offset, inclusive));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -87,8 +77,6 @@ abstract class BaseMappedDocument implements TextDocument {
|
||||
*/
|
||||
protected abstract int localOffsetTransform(int outOffset, boolean inclusive);
|
||||
|
||||
protected abstract int inverseLocalOffsetTransform(int inOffset, boolean inclusive);
|
||||
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
|
@ -4,8 +4,6 @@
|
||||
|
||||
package net.sourceforge.pmd.lang.document;
|
||||
|
||||
import java.util.function.ToIntFunction;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import net.sourceforge.pmd.lang.LanguageVersion;
|
||||
@ -47,74 +45,47 @@ final class FragmentedTextDocument extends BaseMappedDocument implements TextDoc
|
||||
return base.getLanguageVersion();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected int localOffsetTransform(int outOffset, boolean inclusive) {
|
||||
return offsetTransform(outOffset, inclusive,
|
||||
Fragment::outToIn,
|
||||
Fragment::outEnd,
|
||||
Fragment::outStart
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int inverseLocalOffsetTransform(int inOffset, boolean inclusive) {
|
||||
return offsetTransform(inOffset, inclusive,
|
||||
Fragment::inToOut,
|
||||
Fragment::inEnd,
|
||||
Fragment::inStart
|
||||
);
|
||||
}
|
||||
|
||||
interface OffsetMapper {
|
||||
|
||||
int mapOffset(Fragment fragment, int offset);
|
||||
}
|
||||
|
||||
private int offsetTransform(int offset,
|
||||
boolean inclusive,
|
||||
OffsetMapper mapOffsetWhenContains,
|
||||
ToIntFunction<Fragment> end,
|
||||
ToIntFunction<Fragment> start) {
|
||||
// caching the last accessed fragment instead of doing
|
||||
// a linear search is critical for performance.
|
||||
Fragment f = this.lastAccessedFragment;
|
||||
if (f == null) {
|
||||
return offset;
|
||||
return outOffset;
|
||||
}
|
||||
|
||||
// Whether the fragment contains the offset we're looking for.
|
||||
// Will be true most of the time.
|
||||
boolean containsOffset =
|
||||
start.applyAsInt(f) >= offset && offset < end.applyAsInt(f);
|
||||
f.outStart() >= outOffset && outOffset < f.outEnd();
|
||||
|
||||
if (!containsOffset) {
|
||||
// Slow path, we must search for the fragment
|
||||
// This optimisation is important, otherwise we have
|
||||
// to search for very long times in some files
|
||||
|
||||
if (end.applyAsInt(f) < offset) { // search forward
|
||||
while (f.next != null && end.applyAsInt(f) < offset) {
|
||||
if (f.outEnd() < outOffset) { // search forward
|
||||
while (f.next != null && f.outEnd() < outOffset) {
|
||||
f = f.next;
|
||||
}
|
||||
} else { // search backwards
|
||||
while (f.prev != null && offset <= start.applyAsInt(f)) {
|
||||
while (f.prev != null && outOffset <= f.outStart()) {
|
||||
f = f.prev;
|
||||
}
|
||||
}
|
||||
lastAccessedFragment = f;
|
||||
}
|
||||
|
||||
if (!inclusive && end.applyAsInt(f) == offset) {
|
||||
if (!inclusive && f.outEnd() == outOffset) {
|
||||
if (f.next != null) {
|
||||
f = f.next;
|
||||
lastAccessedFragment = f;
|
||||
// fallthrough
|
||||
} else {
|
||||
return mapOffsetWhenContains.mapOffset(f, offset) + 1;
|
||||
return f.outToIn(outOffset) + 1;
|
||||
}
|
||||
}
|
||||
return mapOffsetWhenContains.mapOffset(f, offset);
|
||||
return f.outToIn(outOffset);
|
||||
}
|
||||
|
||||
|
||||
|
@ -180,21 +180,6 @@ public interface TextDocument extends Closeable {
|
||||
*/
|
||||
TextRegion inputRegion(TextRegion outputRegion);
|
||||
|
||||
/**
|
||||
* Translate a 2D range given in the coordinate system of this
|
||||
* document, to the coordinate system of the original document.
|
||||
* This works as if creating a new region with both start and end
|
||||
* offsets translated through {@link #inputOffset(int, boolean)}. The
|
||||
* returned region may have a different length.
|
||||
*
|
||||
* @param outputRange Output region
|
||||
*
|
||||
* @return Input region
|
||||
*/
|
||||
default TextRange2d inputRange(TextRange2d outputRange) {
|
||||
return toRange2d(inputRegion(toRegion(outputRange)));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a reader over the text of this document.
|
||||
@ -218,14 +203,6 @@ public interface TextDocument extends Closeable {
|
||||
return TextRegion.fromOffsetLength(0, getLength());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a 2D text range that corresponds to the entire document,
|
||||
* in the coordinate system of this document.
|
||||
*/
|
||||
default TextRange2d getEntireRegion2d() {
|
||||
return toRange2d(getEntireRegion());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a region that spans the text of all the given lines.
|
||||
* This is intended to provide a replacement for {@link SourceCode#getSlice(int, int)}.
|
||||
@ -257,65 +234,6 @@ public interface TextDocument extends Closeable {
|
||||
*/
|
||||
FileLocation toLocation(TextRegion region);
|
||||
|
||||
/**
|
||||
* Turn a text region into a {@link FileLocation}. The file name is
|
||||
* the display name of this document.
|
||||
*
|
||||
* @return A new file position
|
||||
*
|
||||
* @throws IndexOutOfBoundsException If the argument is not a valid region in this document
|
||||
*/
|
||||
default FileLocation toLocation(TextRange2d range) {
|
||||
int startOffset = offsetAtLineColumn(range.getStartPos());
|
||||
if (startOffset < 0) {
|
||||
throw new IndexOutOfBoundsException("Region out of bounds: " + range.displayString());
|
||||
}
|
||||
TextRegion region = TextRegion.caretAt(startOffset);
|
||||
checkInRange(region, this.getLength());
|
||||
return FileLocation.range(getDisplayName(), range);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a text region to a {@link TextRange2d}.
|
||||
*/
|
||||
default TextRange2d toRange2d(TextRegion region) {
|
||||
TextPos2d start = lineColumnAtOffset(region.getStartOffset(), true);
|
||||
TextPos2d end = lineColumnAtOffset(region.getEndOffset(), false);
|
||||
return TextRange2d.range2d(start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a {@link TextRange2d} into a {@link TextRegion}.
|
||||
*/
|
||||
default TextRegion toRegion(TextRange2d region) {
|
||||
return TextRegion.fromBothOffsets(
|
||||
offsetAtLineColumn(region.getStartPos()),
|
||||
offsetAtLineColumn(region.getEndPos())
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the offset at the given line and column number.
|
||||
*
|
||||
* @param line Line number (1-based)
|
||||
* @param column Column number (1-based)
|
||||
*
|
||||
* @return an offset (0-based)
|
||||
*/
|
||||
int offsetAtLineColumn(int line, int column);
|
||||
|
||||
/**
|
||||
* Returns true if the position is valid in this document.
|
||||
*/
|
||||
boolean isInRange(TextPos2d textPos2d);
|
||||
|
||||
/**
|
||||
* Returns the offset at the line and number.
|
||||
*/
|
||||
default int offsetAtLineColumn(TextPos2d pos2d) {
|
||||
return offsetAtLineColumn(pos2d.getLine(), pos2d.getColumn());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the line and column at the given offset (inclusive).
|
||||
|
@ -11,11 +11,6 @@ import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
*/
|
||||
public final class TextPos2d implements Comparable<TextPos2d> {
|
||||
|
||||
/**
|
||||
* The position at the start of the document (line 1, column 1).
|
||||
*/
|
||||
public static TextPos2d DOCUMENT_START = new TextPos2d(1, 1);
|
||||
|
||||
private final int line;
|
||||
private final int column;
|
||||
|
||||
|
@ -147,7 +147,7 @@ public class TextDocumentTest {
|
||||
}
|
||||
|
||||
private void assertPos2dEqualsAt(TextDocument doc, int offset, String c, TextPos2d pos, boolean inclusive) {
|
||||
Chars slicedChar = doc.sliceText(TextRegion.fromOffsetLength(offset, 1));
|
||||
Chars slicedChar = doc.sliceTranslatedText(TextRegion.fromOffsetLength(offset, 1));
|
||||
assertEquals(c, slicedChar.toString());
|
||||
assertEquals(pos, doc.lineColumnAtOffset(offset, inclusive));
|
||||
}
|
||||
|
@ -2618,14 +2618,14 @@ void AssertStatement() :
|
||||
void RUNSIGNEDSHIFT() #void:
|
||||
{}
|
||||
{
|
||||
LOOKAHEAD({ JavaTokenDocument.getRealKind(getToken(1)) == RUNSIGNEDSHIFT})
|
||||
LOOKAHEAD({ JavaTokenDocumentBehavior.getRealKind(getToken(1)) == RUNSIGNEDSHIFT})
|
||||
">" ">" ">"
|
||||
}
|
||||
|
||||
void RSIGNEDSHIFT() #void:
|
||||
{}
|
||||
{
|
||||
LOOKAHEAD({ JavaTokenDocument.getRealKind(getToken(1)) == RSIGNEDSHIFT})
|
||||
LOOKAHEAD({ JavaTokenDocumentBehavior.getRealKind(getToken(1)) == RSIGNEDSHIFT})
|
||||
">" ">"
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,6 @@ import net.sourceforge.pmd.cpd.token.TokenFilter;
|
||||
import net.sourceforge.pmd.lang.TokenManager;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.CharStream;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken;
|
||||
import net.sourceforge.pmd.lang.document.TextDocument;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccTokenDocument;
|
||||
import net.sourceforge.pmd.lang.java.ast.InternalApiBridge;
|
||||
import net.sourceforge.pmd.lang.java.ast.JavaTokenKinds;
|
||||
@ -44,7 +43,7 @@ public class JavaTokenizer extends JavaCCTokenizer {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JavaccTokenDocument.TokenDocumentBehavior tokenBehavior() throws IOException {
|
||||
protected JavaccTokenDocument.TokenDocumentBehavior tokenBehavior() {
|
||||
return InternalApiBridge.javaTokenDoc();
|
||||
}
|
||||
|
||||
|
@ -160,6 +160,7 @@ package net.sourceforge.pmd.lang.plsql.ast;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import net.sourceforge.pmd.lang.ast.Node;
|
||||
import net.sourceforge.pmd.lang.document.Chars;
|
||||
import net.sourceforge.pmd.lang.ast.impl.javacc.CharStream;
|
||||
import net.sourceforge.pmd.lang.plsql.ast.internal.ParsingExclusion;
|
||||
import net.sourceforge.pmd.lang.ast.TokenMgrError;
|
||||
|
@ -44,7 +44,7 @@ public class ScalaTokenAdapter implements GenericToken<ScalaTokenAdapter> {
|
||||
|
||||
@Override
|
||||
public Chars getImageCs() {
|
||||
return textDocument.sliceText(getRegion());
|
||||
return textDocument.sliceTranslatedText(getRegion());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user