Introduce flag for generating absolute paths
This commit is contained in:
12
Build.java
12
Build.java
@@ -2,7 +2,10 @@ import static nobuild.NoBuild.*;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Build {
|
||||
static final String program = "jtags";
|
||||
@@ -29,7 +32,14 @@ public class Build {
|
||||
|
||||
case "run":
|
||||
buildJtags(mainClass, sourcePaths, classPaths);
|
||||
runJava(classPaths, mainClass, arguments.toArray(String[]::new));
|
||||
|
||||
Map<Boolean, List<String>> argPartition =
|
||||
arguments.stream().collect(Collectors.partitioningBy(it -> it.startsWith("-D")));
|
||||
runJava(
|
||||
classPaths,
|
||||
argPartition.get(true).toArray(String[]::new),
|
||||
mainClass,
|
||||
argPartition.get(false).toArray(String[]::new));
|
||||
break;
|
||||
|
||||
case "package":
|
||||
|
||||
@@ -5,11 +5,14 @@ import static xyz.naofal.jtags.JtagsLogger.logger;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Jtags {
|
||||
static class Options {
|
||||
String[] sources;
|
||||
List<String> sources = new ArrayList<>();
|
||||
boolean absolutePaths = false;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
@@ -21,7 +24,19 @@ public class Jtags {
|
||||
}
|
||||
|
||||
Options options = new Options();
|
||||
options.sources = args;
|
||||
String argument;
|
||||
while ((argument = arguments.poll()) != null) {
|
||||
switch (argument) {
|
||||
case "-absolute":
|
||||
logger.config("Using absolute paths");
|
||||
options.absolutePaths = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
options.sources.add(argument);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
System.exit(run(options) ? 0 : 1);
|
||||
}
|
||||
@@ -29,8 +44,9 @@ public class Jtags {
|
||||
static boolean run(Options options) {
|
||||
var tags = TagCollector.collectTags(options);
|
||||
|
||||
var tagsWriter = new TagsWriter(options);
|
||||
try {
|
||||
TagsWriter.writeTagsFile(tags, new FileOutputStream("tags"));
|
||||
tagsWriter.writeTagsFile(tags, new FileOutputStream("tags"));
|
||||
} catch (FileNotFoundException ex) {
|
||||
logger.severe(ex.toString());
|
||||
return false;
|
||||
@@ -42,7 +58,9 @@ public class Jtags {
|
||||
static void printUsage() {
|
||||
System.err.println(
|
||||
"""
|
||||
USAGE: jtags [options] <sources...>
|
||||
Usage: jtags [options] <sources...>
|
||||
Options:
|
||||
-absolute Use absolute paths for tag locations
|
||||
""");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,6 +37,7 @@ public class JtagsLogger {
|
||||
put(Level.FINEST, 5);
|
||||
put(Level.FINE, 4);
|
||||
put(Level.FINER, 6);
|
||||
put(Level.CONFIG, 4);
|
||||
put(Level.INFO, 2);
|
||||
put(Level.WARNING, 3);
|
||||
put(Level.SEVERE, 1);
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package xyz.naofal.jtags;
|
||||
|
||||
public record Tag(TagKind kind, String name, String location, String line, boolean isStatic)
|
||||
import java.nio.file.Path;
|
||||
|
||||
public record Tag(TagKind kind, String name, Path location, String line, boolean isStatic)
|
||||
implements Comparable<Tag> {
|
||||
|
||||
public Tag(TagKind kind, String name, String location, String line) {
|
||||
public Tag(TagKind kind, String name, Path location, String line) {
|
||||
this(kind, name, location, line, false);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public class TagCollector {
|
||||
try (StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null)) {
|
||||
|
||||
Iterable<? extends JavaFileObject> compilationUnits =
|
||||
fileManager.getJavaFileObjects(options.sources);
|
||||
fileManager.getJavaFileObjects(options.sources.toArray(String[]::new));
|
||||
|
||||
JavacTask task =
|
||||
(JavacTask) compiler.getTask(null, fileManager, null, null, null, compilationUnits);
|
||||
|
||||
@@ -7,11 +7,12 @@ import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.PriorityQueue;
|
||||
import xyz.naofal.jtags.Jtags.Options;
|
||||
|
||||
public class TagsWriter {
|
||||
public record TagsWriter(Options options) {
|
||||
private static final int MAX_PATTERN_LENGTH = 96;
|
||||
|
||||
public static void writeTagsFile(PriorityQueue<Tag> tags, OutputStream outputStream) {
|
||||
public void writeTagsFile(PriorityQueue<Tag> tags, OutputStream outputStream) {
|
||||
var writer = new OutputStreamWriter(outputStream);
|
||||
try {
|
||||
writer.write(
|
||||
@@ -31,10 +32,13 @@ public class TagsWriter {
|
||||
}
|
||||
}
|
||||
|
||||
private static void writeTag(Writer writer, Tag tag) throws IOException {
|
||||
private void writeTag(Writer writer, Tag tag) throws IOException {
|
||||
writer.write(tag.name());
|
||||
writer.write('\t');
|
||||
writer.write(tag.location());
|
||||
writer.write(
|
||||
options().absolutePaths
|
||||
? tag.location().toAbsolutePath().toString()
|
||||
: tag.location().toString());
|
||||
writer.write("\t/^");
|
||||
writer.write(tag.line().substring(0, Math.min(tag.line().length(), MAX_PATTERN_LENGTH)));
|
||||
writer.write("$/;\"\t");
|
||||
|
||||
@@ -84,7 +84,9 @@ public class TreeVisitor extends TreePathScanner<Void, TreeVisitorContext> {
|
||||
p.getLocation(),
|
||||
p.getLine(node),
|
||||
node.getModifiers().getFlags().contains(Modifier.STATIC));
|
||||
|
||||
logger.finer(() -> "Method: " + tag);
|
||||
|
||||
tags.add(tag);
|
||||
|
||||
return super.visitMethod(node, p);
|
||||
@@ -110,7 +112,9 @@ public class TreeVisitor extends TreePathScanner<Void, TreeVisitorContext> {
|
||||
p.getLocation(),
|
||||
p.getLine(node),
|
||||
node.getModifiers().getFlags().contains(Modifier.STATIC));
|
||||
|
||||
logger.finer(() -> "Variable: " + tag);
|
||||
|
||||
tags.add(tag);
|
||||
|
||||
return null;
|
||||
|
||||
@@ -10,6 +10,7 @@ import com.sun.source.tree.Tree;
|
||||
import com.sun.source.util.SourcePositions;
|
||||
import com.sun.source.util.Trees;
|
||||
import java.io.BufferedReader;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Optional;
|
||||
|
||||
@@ -23,12 +24,11 @@ public class TreeVisitorContext {
|
||||
sourcePositions = trees.getSourcePositions();
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
public Path getLocation() {
|
||||
assert compilationUnitTree != null;
|
||||
return Paths.get(".")
|
||||
.toAbsolutePath()
|
||||
.relativize(Paths.get(compilationUnitTree.getSourceFile().toUri()).toAbsolutePath())
|
||||
.toString();
|
||||
.relativize(Paths.get(compilationUnitTree.getSourceFile().toUri()).toAbsolutePath());
|
||||
}
|
||||
|
||||
public String getLine(Tree node) {
|
||||
|
||||
Reference in New Issue
Block a user