/
DeleteLine.m
116 lines (81 loc) · 3.28 KB
/
DeleteLine.m
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
//
// DeleteLine.h
// XcodePlus Delete Line
//
// Created by Pay Liu on 13/9/9.
// Copyright (c) 2013年 Octalord Information Inc. The MIT License.
//
#import "DeleteLine.h"
@interface DeleteLine ()
@property (nonatomic, retain) NSString *deletedText;
@end
#import <AppKit/AppKit.h>
@implementation DeleteLine
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_currentTextView release];
[_deletedText release];
[super dealloc];
}
+ (void) pluginDidLoad:(NSBundle *)plugin
{
static id shared = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [[self alloc] init];
});
}
- (id) init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidFinishLaunching:)
name:NSApplicationDidFinishLaunchingNotification
object:nil];
}
return self;
}
- (void) applicationDidFinishLaunching:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectionDidChange:) name:NSTextViewDidChangeSelectionNotification object:nil];
NSMenuItem *editMenuItem = [[NSApp mainMenu] itemWithTitle:@"Edit"];
if (editMenuItem) {
[[editMenuItem submenu] addItem:[NSMenuItem separatorItem]];
NSMenuItem *newMenuItem = [[NSMenuItem alloc] initWithTitle:@"Delete Line" action:@selector(deleteLine:) keyEquivalent:@"d"];
[newMenuItem setTarget:self];
[newMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask];
[[editMenuItem submenu] addItem:newMenuItem];
[newMenuItem release];
}
}
- (void) selectionDidChange:(NSNotification *)notification
{
if ([[notification object] isKindOfClass:[NSTextView class]]) {
self.currentTextView = (NSTextView *)[notification object];
NSArray *selectedRanges = [self.currentTextView selectedRanges];
if (selectedRanges.count == 0) {
return;
}
NSRange selectedRange = [[selectedRanges objectAtIndex:0] rangeValue];
self.selectedLineRange = [self.currentTextView.textStorage.string lineRangeForRange:selectedRange];
self.deletedText = [self.currentTextView.textStorage.string substringWithRange:self.selectedLineRange];
}
}
- (void) showMessageBox:(NSString *)text
{
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert setMessageText:text];
[alert runModal];
}
- (void) deleteLine:(id)sender
{
// [self showMessageBox:self.deletedText];
if ([self.currentTextView shouldChangeTextInRange:self.selectedLineRange replacementString:@""]) {
[self.currentTextView.textStorage replaceCharactersInRange:self.selectedLineRange
withAttributedString:[[[NSAttributedString alloc] initWithString:@""] autorelease]];
[self.currentTextView didChangeText];
}
}
@end