Skip to content
Snippets Groups Projects
Commit dc9bee9c authored by George Nachman's avatar George Nachman
Browse files

Add a bunch of debug logging for trouter path determination

parent 583c6088
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -9158,6 +9158,9 @@ static double EuclideanDistance(NSPoint p1, NSPoint p2) {
// Remove escaping slashes
NSString *removeEscapingSlashes = @"\\\\([ \\(\\[\\]\\\\)])";
 
DLog(@"Brute force path from prefix <<%@>>, suffix <<%@>> directory=%@",
beforeString, afterString, workingDirectory);
[beforeString replaceOccurrencesOfRegex:removeEscapingSlashes withString:@"$1"];
[afterString replaceOccurrencesOfRegex:removeEscapingSlashes withString:@"$1"];
beforeString = [[beforeString copy] autorelease];
Loading
Loading
@@ -9173,6 +9176,9 @@ static double EuclideanDistance(NSPoint p1, NSPoint p2) {
NSMutableSet *paths = [NSMutableSet set];
NSMutableSet *befores = [NSMutableSet set];
 
DLog(@"before chunks=%@", beforeChunks);
DLog(@"after chunks=%@", afterChunks);
for (int i = [beforeChunks count]; i >= 0; i--) {
NSString *beforeChunk = @"";
if (i < [beforeChunks count]) {
Loading
Loading
@@ -9199,6 +9205,7 @@ static double EuclideanDistance(NSPoint p1, NSPoint p2) {
if (charsTakenFromPrefixPtr) {
*charsTakenFromPrefixPtr = left.length;
}
DLog(@"Using path %@", possiblePath);
return possiblePath;
}
 
Loading
Loading
@@ -9454,6 +9461,7 @@ static double EuclideanDistance(NSPoint p1, NSPoint p2) {
// Don't consider / to be a valid filename because it's useless and single/double slashes are
// pretty common.
if (filename && ![[filename stringByReplacingOccurrencesOfString:@"//" withString:@"/"] isEqualToString:@"/"]) {
DLog(@"Accepting filename from brute force search: %@", filename);
// If you clicked on an existing filename, use it.
URLAction *action = [URLAction urlActionToOpenExistingFile:filename];
action.range = [self coordRangeFromCoord:VT100GridCoordMake(x, y)
Loading
Loading
@@ -9466,6 +9474,7 @@ static double EuclideanDistance(NSPoint p1, NSPoint p2) {
return action;
}
 
DLog(@"Brute force search failed, try smart selection.");
// Next, see if smart selection matches anything with an action.
int tx1, ty1, tx2, ty2;
NSDictionary *rule = [self smartSelectAtX:x
Loading
Loading
@@ -9477,6 +9486,7 @@ static double EuclideanDistance(NSPoint p1, NSPoint p2) {
ignoringNewlines:NO
actionRequired:YES];
NSArray *actions = [SmartSelectionController actionsInRule:rule];
DLog(@" Smart selection produces these actions: %@", actions);
if (actions.count) {
NSString *content = [self contentFromX:tx1
Y:ty1
Loading
Loading
@@ -9485,6 +9495,7 @@ static double EuclideanDistance(NSPoint p1, NSPoint p2) {
pad:NO
includeLastNewline:NO
trimTrailingWhitespace:NO];
DLog(@" Actions match this content: %@", content);
URLAction *action = [URLAction urlActionToPerformSmartSelectionRule:rule onString:content];
action.range = VT100GridCoordRangeMake(tx1, ty1, tx2, ty2);
NSError *regexError;
Loading
Loading
@@ -9501,13 +9512,17 @@ static double EuclideanDistance(NSPoint p1, NSPoint p2) {
// No luck. Look for something vaguely URL-like.
int prefixChars;
NSString *joined = [prefix stringByAppendingString:suffix];
DLog(@"Smart selection found nothing. Look for URL-like things in %@ around offset %d",
joined, (int)[prefix length]);
NSString *possibleUrl = [self stringInString:joined
includingOffset:[prefix length]
fromCharacterSet:[PTYTextView urlCharacterSet]
charsTakenFromPrefix:&prefixChars];
DLog(@"String of just permissible chars is %@", possibleUrl);
NSString *originalMatch = possibleUrl;
int offset, length;
possibleUrl = [self urlInString:possibleUrl offset:&offset length:&length];
DLog(@"URL in string is %@", possibleUrl);
if (!possibleUrl) {
return nil;
}
Loading
Loading
@@ -9516,16 +9531,21 @@ static double EuclideanDistance(NSPoint p1, NSPoint p2) {
if ([possibleUrl rangeOfString:@":"].length > 0) {
NSURL *url = [NSURL URLWithString:possibleUrl];
ruledOutBasedOnScheme = (!url || [[NSWorkspace sharedWorkspace] URLForApplicationToOpenURL:url] == nil);
DLog(@"There seems to be a scheme. ruledOut=%d", (int)ruledOutBasedOnScheme);
}
if ([self _stringLooksLikeURL:[originalMatch substringWithRange:NSMakeRange(offset, length)]] &&
!ruledOutBasedOnScheme) {
DLog(@"%@ looks like a URL and it's not ruled out based on scheme. Go for it.",
[originalMatch substringWithRange:NSMakeRange(offset, length)]);
URLAction *action = [URLAction urlActionToOpenURL:possibleUrl];
action.range = [self coordRangeFromCoord:VT100GridCoordMake(x, y)
startingCharsBefore:prefixChars - offset
length:length];
return action;
} else {
DLog(@"%@ is either not plausibly a URL or was ruled out based on scheme. Fail.",
[originalMatch substringWithRange:NSMakeRange(offset, length)]);
return nil;
}
}
Loading
Loading
Loading
Loading
@@ -26,9 +26,10 @@
*/
 
#import "Trouter.h"
#import "DebugLogging.h"
#import "NSStringITerm.h"
#import "RegexKitLite/RegexKitLite.h"
#import "TrouterPrefsController.h"
#import "NSStringITerm.h"
 
@implementation Trouter
 
Loading
Loading
@@ -109,20 +110,24 @@
workingDirectory:(NSString *)workingDirectory
lineNumber:(NSString **)lineNumber
{
DLog(@"Check if %@ is a valid path in %@", path, workingDirectory);
NSString *origPath = path;
// TODO(chendo): Move regex, define capture semants in config file/prefs
if (!path || [path length] == 0) {
DLog(@" no: it is empty");
return nil;
}
 
// If it's in parens, strip them.
if (path.length > 2 && [path characterAtIndex:0] == '(' && [path hasSuffix:@")"]) {
path = [path substringWithRange:NSMakeRange(1, path.length - 2)];
DLog(@" Strip parens, leaving %@", path);
}
 
// strip various trailing characters that are unlikely to be part of the file name.
path = [path stringByReplacingOccurrencesOfRegex:@"[.),:]$"
withString:@""];
DLog(@" Strip trailing chars, leaving %@", path);
 
if (lineNumber != nil) {
*lineNumber = [path stringByMatching:@":(\\d+)" capture:1];
Loading
Loading
@@ -130,25 +135,30 @@
path = [[path stringByReplacingOccurrencesOfRegex:@":\\d*(?::.*)?$"
withString:@""]
stringByExpandingTildeInPath];
DLog(@" Strip line number suffix leaving %@", path);
if ([path length] == 0) {
// Everything was stripped out, meaning we'd try to open the working directory.
return nil;
}
if ([path rangeOfRegex:@"^/"].location == NSNotFound) {
path = [NSString stringWithFormat:@"%@/%@", workingDirectory, path];
DLog(@" Prepend working directory, giving %@", path);
}
 
NSURL *url = [NSURL fileURLWithPath:path];
 
// Resolve path by removing ./ and ../ etc
path = [[url standardizedURL] path];
DLog(@" Standardized path is %@", path);
 
if ([fileManager fileExistsAtPath:path]) {
DLog(@" YES: A file exists at %@", path);
return path;
}
 
// If path doesn't exist and it starts with "a/" or "b/" (from `diff`).
if ([origPath isMatchedByRegex:@"^[ab]/"]) {
DLog(@" Treating as diff path");
// strip the prefix off ...
origPath = [origPath stringByReplacingOccurrencesOfRegex:@"^[ab]/"
withString:@""];
Loading
Loading
@@ -159,6 +169,7 @@
lineNumber:lineNumber];
}
 
DLog(@" NO: no valid path found");
return nil;
}
 
Loading
Loading
Loading
Loading
@@ -52,4 +52,9 @@
[super dealloc];
}
 
- (NSString *)description {
return [NSString stringWithFormat:@"<%@: %p string=%@ rule=%@>",
[self class], self, self.string, self.rule];
}
@end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment