summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/java/org/javacs/CompileFocus.java2
-rw-r--r--src/main/java/org/javacs/Parser.java1
-rw-r--r--src/main/java/org/javacs/Pruner.java37
-rw-r--r--src/test/java/org/javacs/PrunerTest.java8
4 files changed, 19 insertions, 29 deletions
diff --git a/src/main/java/org/javacs/CompileFocus.java b/src/main/java/org/javacs/CompileFocus.java
index c40ff74..0d73fb9 100644
--- a/src/main/java/org/javacs/CompileFocus.java
+++ b/src/main/java/org/javacs/CompileFocus.java
@@ -31,7 +31,7 @@ public class CompileFocus {
CompileFocus(JavaCompilerService parent, URI file, String contents, int line, int character) {
this.parent = parent;
this.file = file;
- this.contents = new Pruner(file, contents).prune(line, character);
+ this.contents = Pruner.prune(file, contents, line, character);
this.line = line;
this.character = character;
this.task = singleFileTask(parent, file, this.contents);
diff --git a/src/main/java/org/javacs/Parser.java b/src/main/java/org/javacs/Parser.java
index 7e2804f..0ac234f 100644
--- a/src/main/java/org/javacs/Parser.java
+++ b/src/main/java/org/javacs/Parser.java
@@ -117,6 +117,7 @@ class Parser {
// LOG.warning(err.getMessage(Locale.getDefault()));
}
+ // TODO move all this to StringSearch
private static final ByteBuffer SEARCH_BUFFER = ByteBuffer.allocateDirect(1 * 1024 * 1024);
// TODO cache the progress made by searching shorter queries
diff --git a/src/main/java/org/javacs/Pruner.java b/src/main/java/org/javacs/Pruner.java
index 4529da6..08f2553 100644
--- a/src/main/java/org/javacs/Pruner.java
+++ b/src/main/java/org/javacs/Pruner.java
@@ -1,32 +1,21 @@
package org.javacs;
import com.sun.source.tree.*;
-import com.sun.source.util.JavacTask;
import com.sun.source.util.TreeScanner;
import com.sun.source.util.Trees;
import java.io.IOException;
import java.net.URI;
-import java.util.logging.Logger;
class Pruner {
- private static final Logger LOG = Logger.getLogger("main");
-
- private final JavacTask task;
- private final CompilationUnitTree root;
- private final StringBuilder contents;
-
- // TODO this should really just be a static function
- Pruner(URI file, String contents) {
- this.task = Parser.parseTask(new StringFileObject(contents, file));
+ static String prune(URI file, String contents, int line, int character) {
+ var task = Parser.parseTask(new StringFileObject(contents, file));
+ CompilationUnitTree root;
try {
- this.root = task.parse().iterator().next();
+ root = task.parse().iterator().next();
} catch (IOException e) {
throw new RuntimeException(e);
}
- this.contents = new StringBuilder(contents);
- }
-
- String prune(int line, int character) {
+ var buffer = new StringBuilder(contents);
var sourcePositions = Trees.instance(task).getSourcePositions();
var lines = root.getLineMap();
var cursor = lines.getPosition(line, character);
@@ -42,12 +31,12 @@ class Pruner {
void erase(long start, long end) {
for (int i = (int) start; i < end; i++) {
- switch (contents.charAt(i)) {
+ switch (buffer.charAt(i)) {
case '\r':
case '\n':
break;
default:
- contents.setCharAt(i, ' ');
+ buffer.setCharAt(i, ' ');
}
}
}
@@ -57,7 +46,7 @@ class Pruner {
// Erase 'static' keyword so autocomplete works better
if (containsCursor(node) && node.isStatic()) {
var start = (int) sourcePositions.getStartPosition(root, node);
- start = contents.indexOf("static", start);
+ start = buffer.indexOf("static", start);
var end = start + "static".length();
erase(start, end);
}
@@ -73,11 +62,11 @@ class Pruner {
if (!erasedAfterCursor) {
var start = cursor;
var end = sourcePositions.getEndPosition(root, node);
- if (end >= contents.length()) end = contents.length() - 1;
+ if (end >= buffer.length()) end = buffer.length() - 1;
// Find the next line
- while (start < end && contents.charAt((int) start) != '\n') start++;
+ while (start < end && buffer.charAt((int) start) != '\n') start++;
// Find the end of the block
- while (end > start && contents.charAt((int) end) != '}') end--;
+ while (end > start && buffer.charAt((int) end) != '}') end--;
// Erase from next line to end of block
erase(start, end - 1);
erasedAfterCursor = true;
@@ -87,7 +76,7 @@ class Pruner {
var last = node.getStatements().get(node.getStatements().size() - 1);
var start = sourcePositions.getStartPosition(root, first);
var end = sourcePositions.getEndPosition(root, last);
- if (end >= contents.length()) end = contents.length() - 1;
+ if (end >= buffer.length()) end = buffer.length() - 1;
erase(start, end);
}
return null;
@@ -101,6 +90,6 @@ class Pruner {
new Scan().scan(root, null);
- return contents.toString();
+ return buffer.toString();
}
}
diff --git a/src/test/java/org/javacs/PrunerTest.java b/src/test/java/org/javacs/PrunerTest.java
index fc1d163..24098be 100644
--- a/src/test/java/org/javacs/PrunerTest.java
+++ b/src/test/java/org/javacs/PrunerTest.java
@@ -11,28 +11,28 @@ public class PrunerTest {
@Test
public void pruneMethods() {
- var actual = new Pruner(URI.create("/PruneMethods.java"), contents("PruneMethods.java")).prune(6, 19);
+ var actual = Pruner.prune(URI.create("/PruneMethods.java"), contents("PruneMethods.java"), 6, 19);
var expected = contents("PruneMethods_erased.java");
assertThat(actual, equalToIgnoringWhiteSpace(expected));
}
@Test
public void pruneToEndOfBlock() {
- var actual = new Pruner(URI.create("/PruneToEndOfBlock.java"), contents("PruneToEndOfBlock.java")).prune(4, 18);
+ var actual = Pruner.prune(URI.create("/PruneToEndOfBlock.java"), contents("PruneToEndOfBlock.java"), 4, 18);
var expected = contents("PruneToEndOfBlock_erased.java");
assertThat(actual, equalToIgnoringWhiteSpace(expected));
}
@Test
public void pruneMiddle() {
- var actual = new Pruner(URI.create("/PruneMiddle.java"), contents("PruneMiddle.java")).prune(4, 12);
+ var actual = Pruner.prune(URI.create("/PruneMiddle.java"), contents("PruneMiddle.java"), 4, 12);
var expected = contents("PruneMiddle_erased.java");
assertThat(actual, equalToIgnoringWhiteSpace(expected));
}
@Test
public void pruneDot() {
- var actual = new Pruner(URI.create("/PruneDot.java"), contents("PruneDot.java")).prune(3, 11);
+ var actual = Pruner.prune(URI.create("/PruneDot.java"), contents("PruneDot.java"), 3, 11);
var expected = contents("PruneDot_erased.java");
assertThat(actual, equalToIgnoringWhiteSpace(expected));
}