This repository was archived by the owner on Dec 12, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathXSourceNoteUtil.m
More file actions
executable file
·196 lines (155 loc) · 6.85 KB
/
Copy pathXSourceNoteUtil.m
File metadata and controls
executable file
·196 lines (155 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// XcodeUtil.m
// XSourceNote
//
// Created by everettjf on 9/29/15.
// Copyright © 2015 everettjf. All rights reserved.
//
#import "XSourceNoteUtil.h"
#import <objc/runtime.h>
@implementation XSourceNoteGlobal
+(XSourceNoteGlobal *)shared{
static XSourceNoteGlobal *inst;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
inst = [[XSourceNoteGlobal alloc]init];
});
return inst;
}
@end
@implementation XSourceNoteUtil
+ (IDEWorkspaceTabController*)tabController
{
NSWindowController* currentWindowController = [XSourceNoteUtil currentIDEWorkspaceWindowController];
if ([currentWindowController
isKindOfClass:NSClassFromString(@"IDEWorkspaceWindowController")]) {
IDEWorkspaceWindowController* workspaceController = (IDEWorkspaceWindowController*)currentWindowController;
return workspaceController.activeWorkspaceTabController;
}
return nil;
}
+ (id)currentEditor
{
NSWindowController* currentWindowController = [XSourceNoteUtil currentIDEWorkspaceWindowController];
if ([currentWindowController
isKindOfClass:NSClassFromString(@"IDEWorkspaceWindowController")]) {
IDEWorkspaceWindowController* workspaceController = (IDEWorkspaceWindowController*)currentWindowController;
IDEEditorArea* editorArea = [workspaceController editorArea];
IDEEditorContext* editorContext = [editorArea lastActiveEditorContext];
return [editorContext editor];
}
return nil;
}
+ (IDEWorkspace*)currentIDEWorkspace {
return (IDEWorkspace*) [[XSourceNoteUtil currentIDEWorkspaceWindowController] valueForKey:@"_workspace"];
}
+ (IDEWorkspaceDocument*)currentWorkspaceDocument
{
NSWindowController* currentWindowController = [XSourceNoteUtil currentIDEWorkspaceWindowController];
id document = [currentWindowController document];
if (currentWindowController &&
[document isKindOfClass:NSClassFromString(@"IDEWorkspaceDocument")]) {
return (IDEWorkspaceDocument*)document;
}
return nil;
}
+ (IDESourceCodeDocument*)currentSourceCodeDocument
{
IDESourceCodeEditor* editor = [self currentEditor];
if ([editor isKindOfClass:NSClassFromString(@"IDESourceCodeEditor")]) {
return editor.sourceCodeDocument;
}
if ([editor
isKindOfClass:NSClassFromString(@"IDESourceCodeComparisonEditor")]) {
if ([[(IDESourceCodeComparisonEditor*)editor primaryDocument]
isKindOfClass:NSClassFromString(@"IDESourceCodeDocument")]) {
return (id)[(IDESourceCodeComparisonEditor*)editor primaryDocument];
}
}
return nil;
}
+(NSString *)currentWorkspaceFilePath{
IDEWorkspaceDocument *document = [XSourceNoteUtil currentWorkspaceDocument];
if(nil == document)
return nil;
DVTFilePath *workspacefilePath = document.workspace.representingFilePath;
return [[workspacefilePath.fileURL absoluteString] stringByRemovingPercentEncoding];
}
+ (void)highlightLine:(NSUInteger)lineNumber inTextView:(NSTextView*)textView
{
--lineNumber;
NSString* text = [textView string];
NSRegularExpression* re =
[NSRegularExpression regularExpressionWithPattern:@"\n"
options:0
error:nil];
NSArray* result = [re matchesInString:text
options:NSMatchingReportCompletion
range:NSMakeRange(0, text.length)];
if (result.count <= lineNumber) {
return;
}
NSUInteger location = 0;
NSTextCheckingResult* aim = result[lineNumber];
location = aim.range.location;
NSRange range = [text lineRangeForRange:NSMakeRange(location, 0)];
[textView scrollRangeToVisible:range];
[textView setSelectedRange:range];
}
+ (IDEWorkspaceWindowController*)currentIDEWorkspaceWindowController {
if([XSourceNoteGlobal shared].mainWorkspaceWindowController == nil){
[XSourceNoteGlobal shared].mainWorkspaceWindowController = (IDEWorkspaceWindowController *)[[NSApp mainWindow]windowController];
}
return [XSourceNoteGlobal shared].mainWorkspaceWindowController;
}
+ (void)jumpToFileURL:(NSURL *)fileURL {
DVTDocumentLocation *documentLocation = [[DVTDocumentLocation alloc] initWithDocumentURL:fileURL timestamp:nil];
IDEEditorOpenSpecifier *openSpecifier = [IDEEditorOpenSpecifier structureEditorOpenSpecifierForDocumentLocation:documentLocation inWorkspace:[XSourceNoteUtil currentIDEWorkspace]
error:nil];
[[XSourceNoteUtil currentIDEWorkspaceWindowController].editorArea.lastActiveEditorContext openEditorOpenSpecifier:openSpecifier];
}
+ (NSUInteger)locationRangeForTextView:(DVTSourceTextView*)textView forLine:(NSUInteger)lineNumber {
DVTTextStorage *textStorage = (DVTTextStorage*)textView.textStorage;
NSRange characterRange = [textStorage characterRangeForLineRange:NSMakeRange(lineNumber, 0)];
return characterRange.location;
}
+(BOOL)openSourceFile:(NSString *)sourceFilePath highlightLineNumber:(NSUInteger)lineNumber{
if(!sourceFilePath) return NO;
IDESourceCodeDocument *document = [XSourceNoteUtil currentSourceCodeDocument];
if(!document.fileURL)return NO;
NSString *currentPath = document.fileURL.path;
if(!currentPath)return NO;
if(![sourceFilePath isEqualToString:currentPath]){
[self jumpToFileURL:[NSURL fileURLWithPath:sourceFilePath]];
}
IDESourceCodeEditor *editor = [XSourceNoteUtil currentEditor];
if (editor) {
DVTSourceTextView* textView = editor.textView;
NSUInteger lineLocation = [XSourceNoteUtil locationRangeForTextView:textView forLine:lineNumber-1];
NSRange locationRange = NSMakeRange(lineLocation, 0);
[textView setSelectedRange:locationRange];
[textView scrollRangeToVisible:locationRange];
[textView showFindIndicatorForRange:locationRange];
}
return YES;
}
+ (NSString*)settingDirectory
{
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString* settingDirectory = [(NSString*)[paths firstObject] stringByAppendingPathComponent:@"XSourceNote"];
NSFileManager *fileManger = [NSFileManager defaultManager];
if (![fileManger fileExistsAtPath:settingDirectory]) {
[fileManger createDirectoryAtPath:settingDirectory withIntermediateDirectories:YES attributes:nil error:nil];
}
return settingDirectory;
}
+ (NSString *)notesDirectory{
NSString *settingDir = [XSourceNoteUtil settingDirectory];
NSString *notesDir = [settingDir stringByAppendingPathComponent:@"Notes"];
NSFileManager *fileManger = [NSFileManager defaultManager];
if (![fileManger fileExistsAtPath:notesDir]) {
[fileManger createDirectoryAtPath:notesDir withIntermediateDirectories:YES attributes:nil error:nil];
}
return notesDir;
}
@end