forked from everettjf/XSourceNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXSourceNote.m
More file actions
executable file
·196 lines (161 loc) · 7.3 KB
/
Copy pathXSourceNote.m
File metadata and controls
executable file
·196 lines (161 loc) · 7.3 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
//
// XSourceNote.m
// XSourceNote
//
// Created by everettjf on 16/2/6.
// Copyright © 2016年 everettjf. All rights reserved.
//
#import "XSourceNote.h"
#import "XSourceNoteUtil.h"
#import "XSourceNoteModel.h"
#import "XSourceNoteWindowController.h"
#import "XSourceNoteQuickNoteWindowController.h"
#import "XSourceNoteDefaults.h"
#import "XSourceNoteStorage.h"
@interface XSourceNote()
@property (nonatomic, strong, readwrite) NSBundle *bundle;
@property (nonatomic, strong) XSourceNoteWindowController *windowController;
@property (nonatomic, strong) XSourceNoteQuickNoteWindowController *quickNote;
@end
@implementation XSourceNote
+ (instancetype)sharedPlugin
{
return sharedPlugin;
}
- (id)initWithBundle:(NSBundle *)plugin
{
if (self = [super init]) {
// reference to plugin's bundle, for resource access
self.bundle = plugin;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didApplicationFinishLaunchingNotification:)
name:NSApplicationDidFinishLaunchingNotification
object:nil];
}
return self;
}
- (void)didApplicationFinishLaunchingNotification:(NSNotification*)noti
{
//removeObserver
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationDidFinishLaunchingNotification object:nil];
NSMenuItem *menuItem = [[NSApp mainMenu] itemWithTitle:@"Edit"];
if (menuItem) {
[[menuItem submenu] addItem:[NSMenuItem separatorItem]];
NSMenuItem *mainMenu = [[menuItem submenu]addItemWithTitle:@"XSourceNote" action:nil keyEquivalent:@""];
NSMenu *submenu = [[NSMenu alloc]init];
mainMenu.submenu = submenu;
{
XSN_MAXShortcut *shortcut = [XSourceNoteDefaults sharedDefaults].currentShortcutToggle;
NSMenuItem *actionMenuItem = [[NSMenuItem alloc] initWithTitle:@"Toggle Note" action:@selector(toggleNote)
keyEquivalent:shortcut.keyCodeStringForKeyEquivalent];
[actionMenuItem setKeyEquivalentModifierMask:shortcut.modifierFlags];
[actionMenuItem setTarget:self];
[[mainMenu submenu] addItem:actionMenuItem];
[XSourceNoteDefaults sharedDefaults].toggleMenuItem = actionMenuItem;
}
{
XSN_MAXShortcut *shortcut = [XSourceNoteDefaults sharedDefaults].currentShortcutShow;
NSMenuItem *actionMenuItem = [[NSMenuItem alloc] initWithTitle:@"Show Notes" action:@selector(showNotes)
keyEquivalent:shortcut.keyCodeStringForKeyEquivalent];
[actionMenuItem setKeyEquivalentModifierMask:shortcut.modifierFlags];
[actionMenuItem setKeyEquivalentModifierMask:NSShiftKeyMask];
[actionMenuItem setTarget:self];
[[mainMenu submenu] addItem:actionMenuItem];
[XSourceNoteDefaults sharedDefaults].showMenuItem = actionMenuItem;
}
}
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)alertWorkspaceInvalid{
NSAlert *alert = [[NSAlert alloc]init];
[alert addButtonWithTitle:@"OK"];
[alert setInformativeText:@"Please open a workspace."];
[alert runModal];
}
- (void)toggleNote
{
[[XSourceNoteModel sharedModel]ensureInit];
if(![[XSourceNoteStorage sharedStorage]isValid]){
[self alertWorkspaceInvalid];
return;
}
IDESourceCodeEditor* editor = [XSourceNoteUtil currentEditor];
if(!editor)return;
if ([editor isKindOfClass:[IDEEditorEmpty class]]) return;
NSTextView* textView = editor.textView;
if (!textView)return;
NSRange range = [textView.selectedRanges[0] rangeValue];
NSUInteger startLineNumber = [[[textView string]substringToIndex:range.location]componentsSeparatedByString:@"\n"].count;
NSUInteger endLineNumber = startLineNumber;
if(range.length > 0){
endLineNumber = [[[textView string]substringToIndex:range.location + range.length]componentsSeparatedByString:@"\n"].count;
}
// NSLog(@"range = (%@,%@) , startLine = %@, endLine = %@",
// @(range.location), @(range.length), @(startLineNumber),@(endLineNumber));
NSRange lineRange = [[textView string]lineRangeForRange:range];
// NSLog(@"line range = (%@,%@)", @(range.location),@(range.length));
NSString *codeOfLines = [[textView string]substringWithRange:lineRange];
// NSLog(@"code below = \n%@", codeOfLines);
// length of "file://" is 7
NSString *sourcePath = [[editor.sourceCodeDocument.fileURL absoluteString] substringFromIndex:7];
XSourceNoteStorage *st = [XSourceNoteStorage sharedStorage];
if([st.rootPath isEqualToString:@""]){
NSAlert *alert = [[NSAlert alloc]init];
[alert addButtonWithTitle:@"OK"];
[alert setInformativeText:@"Please select the project root path first."];
[alert runModal];
return;
}
NSRange relativeRange = [sourcePath rangeOfString:st.rootPath];
if(relativeRange.location != 0){
NSAlert *alert = [[NSAlert alloc]init];
[alert addButtonWithTitle:@"OK"];
[alert setInformativeText:@"Current source is not under your root path, or your root path is incorrect."];
[alert runModal];
// return;
}
NSString *relativePath = [sourcePath stringByReplacingOccurrencesOfString:st.rootPath withString:@""];
XSourceNoteLineEntity *lineNote = [XSourceNoteLineEntity new];
lineNote.source = relativePath;
lineNote.begin = startLineNumber;
lineNote.end = endLineNumber;
lineNote.uniqueID = [NSString stringWithFormat:@"%@_%@_%@",lineNote.source,@(lineNote.begin),@(lineNote.end)];
lineNote.code = codeOfLines;
lineNote.localPath = sourcePath;
[[XSourceNoteModel sharedModel]addLineNote:lineNote];
NSView *sidebar = [editor valueForKey:@"_sidebarView"];
if(sidebar)[sidebar setNeedsDisplay:YES];
if(!self.quickNote){
self.quickNote = [[XSourceNoteQuickNoteWindowController alloc]init];
self.quickNote.line = lineNote;
[self.quickNote.window makeKeyAndOrderFront:nil];
[self.quickNote refresh];
}else{
[self.quickNote saveCurrentContent];
self.quickNote.line = lineNote;
[self.quickNote refresh];
[self.quickNote.window orderFront:nil];
}
}
- (void)showNotes{
[[XSourceNoteModel sharedModel]ensureInit];
if(![[XSourceNoteStorage sharedStorage]isValid]){
[self alertWorkspaceInvalid];
return;
}
if(self.windowController.window.isVisible){
[self.windowController.window close];
}else{
if(self.windowController == nil){
// Remember the current IDE workspace window controller
[XSourceNoteUtil currentIDEWorkspaceWindowController];
self.windowController = [[XSourceNoteWindowController alloc]initWithWindowNibName:@"XSourceNoteWindowController"];
}
self.windowController.window.title = [[XSourceNoteUtil currentWorkspaceDocument].displayName stringByDeletingLastPathComponent];
[self.windowController.window makeKeyAndOrderFront:nil];
}
}
@end