从 XCode 4.2 外部跳转到文件和行

发布于 2024-12-13 02:53:11 字数 696 浏览 4 评论 0原文

对于 NSLogger 项目,我们希望实现直接跳回 XCode 到发出日志条目的文件中的行的功能。人们会期望使用这样的命令行工具很容易做到这一点:

xed --line 100 ~/work/xyz/MainWindowController.m

但这会导致意外错误:

2011-10-31 17:37:36.159 xed[53507:707] 错误:错误 Domain=NSOSStatusErrorDomain Code=-1728“该操作无法 完全的。 (OSStatus 错误 -1728。)”(例如:说明符要求 第 3 个,但只有 2 个。基本上,这表示运行时 分辨率错误。 ) UserInfo=0x40043dc20 {ErrorNumber=-1728, ErrorOffendingObject=}

另一个想法是使用 AppleScript 告诉 XCode 执行所需的步骤,但我无法找到可行的解决方案。

因此,任何能够达到预期效果的解决方案都将非常感激。

参考 GitHub 上的 NSLogger 问题:https://github.com/fpillet/NSLogger/issues/30

For the NSLogger project we would like to implement the feature to directly jump back to XCode to the line in the file that issued the log entry. One would expect thatthis would be easy using the command line tool like this:

xed --line 100 ~/work/xyz/MainWindowController.m

But this results in an unexpected error:

2011-10-31 17:37:36.159 xed[53507:707] Error: Error
Domain=NSOSStatusErrorDomain Code=-1728 "The operation couldn’t be
completed. (OSStatus error -1728.)" (e.g.,: specifier asked for the
3rd, but there are only 2. Basically, this indicates a run-time
resolution error. ) UserInfo=0x40043dc20 {ErrorNumber=-1728,
ErrorOffendingObject=}

Another idea is to use AppleScript to tell XCode to do the desired steps, but I was not able to find a working solution.

So any solution to reach the desired effect would be very appreciated.

Reference to the NSLogger Issue on GitHub: https://github.com/fpillet/NSLogger/issues/30

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如若梦似彩虹 2024-12-20 02:53:11

xed 工具似乎工作正常:

xed --line 100 /Users/Anne/Desktop/Test/TestAppDelegate.m

错误

例如:说明符要求第 3 个,但只有 2 个

上面的错误只是表明请求的行超出范围。

解决方案

在执行xed之前检查行号是否确实存在。

快速编写的示例

// Define file and line number
NSString *filePath = @"/Users/Anne/Desktop/Test/TestAppDelegate.m"; 
int theLine = 100;

// Count lines in file
NSString *fileContent = [[NSString alloc] initWithContentsOfFile: filePath];
unsigned numberOfLines, index, stringLength = [fileContent length];
for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
    index = NSMaxRange([fileContent lineRangeForRange:NSMakeRange(index, 0)]);

// The requested line does not exist
if (theLine > numberOfLines) {
    NSLog(@"Error: The requested line is out of range.");

// The requested line exists
} else {

    // Run xed through AppleScript or NSTask
    NSString *theSource = [NSString stringWithFormat: @"do shell script \"xed --line %d \" & quoted form of \"%@\"", theLine, filePath];        
    NSAppleScript *theScript = [[NSAppleScript alloc] initWithSource:theSource];
    [theScript executeAndReturnError:nil];

}

注意

确保正确计算行数:
计算文本行数

The xed tool seems to be working fine:

xed --line 100 /Users/Anne/Desktop/Test/TestAppDelegate.m

Error

e.g.,: specifier asked for the 3rd, but there are only 2

The error above simply indicates that the requested line is out of range.

Solution

Check wether the line number actually exists before executing xed.

Quickly written example

// Define file and line number
NSString *filePath = @"/Users/Anne/Desktop/Test/TestAppDelegate.m"; 
int theLine = 100;

// Count lines in file
NSString *fileContent = [[NSString alloc] initWithContentsOfFile: filePath];
unsigned numberOfLines, index, stringLength = [fileContent length];
for (index = 0, numberOfLines = 0; index < stringLength; numberOfLines++)
    index = NSMaxRange([fileContent lineRangeForRange:NSMakeRange(index, 0)]);

// The requested line does not exist
if (theLine > numberOfLines) {
    NSLog(@"Error: The requested line is out of range.");

// The requested line exists
} else {

    // Run xed through AppleScript or NSTask
    NSString *theSource = [NSString stringWithFormat: @"do shell script \"xed --line %d \" & quoted form of \"%@\"", theLine, filePath];        
    NSAppleScript *theScript = [[NSAppleScript alloc] initWithSource:theSource];
    [theScript executeAndReturnError:nil];

}

Note

Make sure to count the number of lines correctly:
Counting Lines of Text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文