Skip to content

Commit

Permalink
be more lenient on matching path separators
Browse files Browse the repository at this point in the history
  • Loading branch information
softwareCobbler committed Aug 23, 2024
1 parent 668657c commit 3af5f75
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 44 deletions.
54 changes: 23 additions & 31 deletions luceedebug/src/main/java/luceedebug/Config.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package luceedebug;

import java.io.File;

public class Config {
private final boolean fsIsCaseSensitive_;
// we probably never want to step into this (the a=b in `function foo(a=b) { ... }` )
// but for now it's configurable
private boolean stepIntoUdfDefaultValueInitFrames_ = false;

Config(boolean fsIsCaseSensitive) {
this.fsIsCaseSensitive_ = fsIsCaseSensitive;
}

public boolean getStepIntoUdfDefaultValueInitFrames() {
return this.stepIntoUdfDefaultValueInitFrames_;
}
public void setStepIntoUdfDefaultValueInitFrames(boolean v) {
this.stepIntoUdfDefaultValueInitFrames_ = v;
}

private static String invertCase(String path) {
int offset = 0;
int strLen = path.length();

public class Config {
private final boolean fsIsCaseSensitive_;
// we probably never want to step into this (the a=b in `function foo(a=b) { ... }` )
// but for now it's configurable
private boolean stepIntoUdfDefaultValueInitFrames_ = false;

Config(boolean fsIsCaseSensitive) {
this.fsIsCaseSensitive_ = fsIsCaseSensitive;
}

public boolean getStepIntoUdfDefaultValueInitFrames() {
return this.stepIntoUdfDefaultValueInitFrames_;
}
public void setStepIntoUdfDefaultValueInitFrames(boolean v) {
this.stepIntoUdfDefaultValueInitFrames_ = v;
}

private static String invertCase(String path) {
int offset = 0;
int strLen = path.length();
final var builder = new StringBuilder();
while (offset < strLen) {
int c = path.codePointAt(offset);
Expand Down Expand Up @@ -50,16 +50,8 @@ public boolean getFsIsCaseSensitive() {
return fsIsCaseSensitive_;
}

public String canonicalizePath(String path) {
if (fsIsCaseSensitive_) {
return path;
}
else {
return path.toLowerCase();
}
public static String canonicalizeFileName(String s) {
return s.replaceAll("[\\\\/]+", "/").toLowerCase();
}

public OriginalAndTransformedString canonicalizedPath(String path) {
return new OriginalAndTransformedString(path, canonicalizePath(path));
}
}
20 changes: 14 additions & 6 deletions luceedebug/src/main/java/luceedebug/DapServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,29 @@ interface TransformRunner {
* if no transform matches, returns the input string unmodified
*/
private String applyPathTransforms(String s, TransformRunner runner) {
var canonicalized = Config.canonicalizeFileName(s);
for (var transform : pathTransforms) {
var result = runner.run(transform, s);
var result = runner.run(transform, canonicalized);
if (result.isPresent()) {
return config_.canonicalizePath(result.get());
return result.get();
}
}
// no transform matched, but still needs canonicalization
return config_.canonicalizePath(s);
return canonicalized;
}

private String applyPathTransformsIdeToCf(String s) {
return applyPathTransforms(
var transformed = applyPathTransforms(
s,
(transform, path) -> transform.ideToServer(path)
).replaceAll("\\\\|/", Matcher.quoteReplacement(File.separator));
);

// recanonicalize after transform, which may have used a non-canonical replacement
return Config.canonicalizeFileName(transformed);
}

private String applyPathTransformsCfToIde(String s) {
// n.b. do _not_ recanonicalize when sending back to IDE
return applyPathTransforms(
s,
(transform, path) -> transform.serverToIde(path)
Expand Down Expand Up @@ -385,7 +390,10 @@ public CompletableFuture<VariablesResponse> variables(VariablesArguments args) {

@Override
public CompletableFuture<SetBreakpointsResponse> setBreakpoints(SetBreakpointsArguments args) {
final var path = new OriginalAndTransformedString(args.getSource().getPath(), applyPathTransformsIdeToCf(args.getSource().getPath()));
final var path = new OriginalAndTransformedString(
args.getSource().getPath(),
applyPathTransformsIdeToCf(args.getSource().getPath())
);
logger.finest("bp for " + path.original + " -> " + path.transformed);
final int size = args.getBreakpoints().length;
final int[] lines = new int[size];
Expand Down
23 changes: 17 additions & 6 deletions luceedebug/src/main/java/luceedebug/PrefixPathTransform.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
package luceedebug;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class PrefixPathTransform implements IPathTransform {
private final String idePrefix_;
private final String serverPrefix_;
private final Pattern idePrefixPattern_;
private final Pattern serverPrefixPattern_;

public PrefixPathTransform(String idePrefix, String serverPrefix) {
this.idePrefix_ = idePrefix;
this.idePrefixPattern_ = asCanonicalFilePrefixPattern(idePrefix);
this.serverPrefix_ = serverPrefix;
this.serverPrefixPattern_ = asCanonicalFilePrefixPattern(serverPrefix);
}

static Pattern asCanonicalFilePrefixPattern(String s) {
return Pattern.compile("(?i)^" + Pattern.quote(Config.canonicalizeFileName(s)));
}

public Optional<String> serverToIde(String s) {
return replacePrefix(s, serverPrefix_, idePrefix_);
return replacePrefix(s, serverPrefixPattern_, idePrefix_);
}
public Optional<String> ideToServer(String s) {
return replacePrefix(s, idePrefix_, serverPrefix_);
return replacePrefix(s, idePrefixPattern_, serverPrefix_);
}

public String asTraceString() {
return "PrefixPathTransform{idePrefix='" + idePrefix_ + "', serverPrefix='" + serverPrefix_ + "'}";
}

private static Optional<String> replacePrefix(String s, String prefixToReplace, String newPrefix) {
// first we check that the string starts with the prefixToReplace in a case insensitive way
if (s.toLowerCase().startsWith(prefixToReplace.toLowerCase())) {
String path = newPrefix + s.substring(prefixToReplace.length());
private static Optional<String> replacePrefix(String s, Pattern prefixToReplace, String prefix) {
var canonicalizedS = Config.canonicalizeFileName(s);
var m = prefixToReplace.matcher(canonicalizedS);
if (m.find()) {
String path = m.replaceFirst(Matcher.quoteReplacement(prefix));
return Optional.of(path);
}
return Optional.empty();
Expand Down
5 changes: 4 additions & 1 deletion luceedebug/src/main/java/luceedebug/coreinject/LuceeVm.java
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,10 @@ private KlassMap(Config config, ReferenceType refType) throws AbsentInformationE
lineMap.put(loc.lineNumber(), loc);
}

this.sourceName = config.canonicalizedPath(sourceName);
this.sourceName = new OriginalAndTransformedString(
sourceName,
Config.canonicalizeFileName(sourceName)
);
this.lineMap = lineMap;
this.refType = refType;
}
Expand Down

0 comments on commit 3af5f75

Please sign in to comment.