forked from onevcat/VVDocumenter-Xcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVVDocumenterManager.m
More file actions
86 lines (70 loc) · 3.27 KB
/
VVDocumenterManager.m
File metadata and controls
86 lines (70 loc) · 3.27 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
//
// VVDocumenterManager.m
// VVDocumenter-Xcode
//
// Created by 王 巍 on 13-7-16.
// Copyright (c) 2013年 OneV's Den. All rights reserved.
//
#import "VVDocumenterManager.h"
#import "NSTextView+VVTextGetter.h"
#import "NSString+VVSyntax.h"
#import "VVDocumenter.h"
#import "XcodePrivate.h"
@implementation VVDocumenterManager
+(void)pluginDidLoad:(NSBundle *)plugin {
VVLog(@"VVDocumenter: Plugin loaded successfully");
[self shared];
}
+(id) shared {
static dispatch_once_t once;
static id instance = nil;
dispatch_once(&once, ^{
instance = [[self alloc] init];
});
return instance;
}
- (id)init {
if (self = [super init]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidFinishLaunching:)
name:NSApplicationDidFinishLaunchingNotification
object:nil];
}
return self;
}
- (void) applicationDidFinishLaunching: (NSNotification*) noti {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textStorageDidChanged:)
name:NSTextDidChangeNotification
object:nil];
}
- (void) textStorageDidChanged:(NSNotification *)noti {
if ([[noti object] isKindOfClass:[NSTextView class]]) {
NSTextView *textView = (NSTextView *)[noti object];
VVTextResult *currentLineResult = [textView textResultOfCurrentLine];
if (currentLineResult) {
if ([currentLineResult.string matchesPatternRegexPattern:@"^\\s*///"]) {
//Get a @"///". Do work!
//Decide which is closer to the cursor. A semicolon or a half brace.
//We just want to document the next valid line.
VVTextResult *resultUntilSemiColon = [textView textResultUntilNextString:@";"];
VVTextResult *resultUntilBrace = [textView textResultUntilNextString:@"{"];
VVTextResult *resultToDocument = nil;
if (resultUntilSemiColon && resultUntilBrace) {
resultToDocument = (resultUntilSemiColon.range.length < resultUntilBrace.range.length) ? resultUntilSemiColon : resultUntilBrace;
} else if (resultUntilBrace) {
resultToDocument = resultUntilBrace;
} else {
resultToDocument = resultUntilSemiColon;
}
VVDocumenter *doc = [[VVDocumenter alloc] initWithCode:resultToDocument.string];
DVTSourceTextStorage *sts = (DVTSourceTextStorage *)textView.textStorage;
[sts replaceCharactersInRange:currentLineResult.range withString:[doc document] withUndoManager:[textView undoManager]];
//Set cursor before the inserted documentation. So we can use tab to begin edit.
int baseIndentationLength = (int)[doc baseIndentation].length;
[textView setSelectedRange:NSMakeRange(currentLineResult.range.location + baseIndentationLength, 0)];
}
}
}
}
@end