Repository navigation
Expand file tree
/
Copy pathCodeWindowHandler.cs
More file actions
186 lines (163 loc) · 6.76 KB
/
Copy pathCodeWindowHandler.cs
File metadata and controls
186 lines (163 loc) · 6.76 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
#region License
/*
EventHandler Naming Visual Studio Extension
Copyright (C) 2010 Einar Egilsson
http://einaregilsson.com/better-eventhandler-names-in-visual-studio-2010/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
$Id$
*/
#endregion
using EnvDTE80;
using EnvDTE;
using System.Text.RegularExpressions;
namespace EinarEgilsson.EventHandlerNaming
{
internal class CodeWindowHandler
{
private readonly TextDocumentKeyPressEvents _textDocKeyPressEvents;
private readonly DTE2 _application;
private readonly PatternNameProvider _nameProvider;
private readonly Options _options;
internal CodeWindowHandler(DTE2 application, PatternNameProvider nameProvider, Options options)
{
_application = application;
_textDocKeyPressEvents = ((Events2)application.Events).TextDocumentKeyPressEvents[null];
_textDocKeyPressEvents.AfterKeyPress += AfterTextDocumentKeyPress;
_textDocKeyPressEvents.BeforeKeyPress += BeforeTextDocumentKeyPress;
_nameProvider = nameProvider;
_options = options;
}
//Variables that hold state between beforekeypress and afterkeypress
private bool _isRenameCandidate;
private bool _isExpandingCandidate;
private string _eventName;
private EditPoint _delegateStart;
private bool _openedUndoContext;
void BeforeTextDocumentKeyPress(string keypress, TextSelection selection, bool inStatementCompletion, ref bool cancelKeypress)
{
if (keypress != "\t" || _application.ActiveDocument.Language != "CSharp")
{
return;
}
if (HandleCreateDelegateTemplate(selection))
{
return;
}
HandleExpandDelegateTemplate(selection);
}
private void StartUndoOperation()
{
if (!_application.UndoContext.IsOpen)
{
_application.UndoContext.Open("EventHandlerNaming");
_openedUndoContext = true;
}
}
private bool HandleCreateDelegateTemplate(TextSelection selection)
{
var editPoint = selection.TopPoint.CreateEditPoint();
editPoint.StartOfLine();
string previousLine = editPoint.GetText(selection.TopPoint);
Match matchCreatingEventHandler = Regex.Match(previousLine, @"\.(\w+)\s*\+=\s*$");
_isRenameCandidate = matchCreatingEventHandler.Success;
if (_isRenameCandidate)
{
_eventName = matchCreatingEventHandler.Groups[1].Value;
StartUndoOperation();
return true;
}
return false;
}
private void HandleExpandDelegateTemplate(TextSelection selection)
{
if (!_options.UseDelegateInference)
{
return;
}
var editPoint = selection.TopPoint.CreateEditPoint();
editPoint.StartOfLine();
string previousLine = editPoint.GetText(selection.TopPoint);
Match matchExpandingEventHandler = Regex.Match(previousLine, @"\.\w+\s*\+=\s*new\s+[a-zA-Z0-9_\.]+\s*\((\w*)$");
_isExpandingCandidate = matchExpandingEventHandler.Success;
if (_isExpandingCandidate)
{
StartUndoOperation();
_delegateStart = selection.TopPoint.CreateEditPoint();
_delegateStart.CharLeft(matchExpandingEventHandler.Groups[1].Value.Length); //We are now right after the (
}
}
private void AfterTextDocumentKeyPress(string keypress, TextSelection selection, bool inStatementCompletion)
{
if (!_isRenameCandidate && !_isExpandingCandidate)
{
return;
}
try
{
if (_isRenameCandidate)
{
DoRename(selection);
}
if (_isExpandingCandidate)
{
DoExpand(selection);
}
if (_openedUndoContext)
{
_application.UndoContext.Close();
}
}
finally
{
_isRenameCandidate = false;
_isExpandingCandidate = false;
_openedUndoContext = false;
}
}
private void DoRename(TextSelection selection)
{
if (selection.Text.Length == 0 || !selection.Text.Contains("_" + _eventName))
{
return;
}
//Now look in more detail and figure out if this actually is what we're looking for
string original = selection.Text;
int lastPost = original.LastIndexOf('_');
string siteName = original.Substring(0, lastPost);
string eventName = original.Substring(lastPost + 1);
var classElement = selection.ActivePoint.CodeElement[vsCMElement.vsCMElementClass];
string name = _nameProvider.CreateEventHandlerName(siteName, eventName, classElement.Name, selection.Text);
selection.TopPoint.CreateEditPoint().ReplaceText(selection.BottomPoint, name, 0);
}
private void DoExpand(TextSelection selection)
{
if (_delegateStart == null)
{
return;
}
var point = selection.BottomPoint.CreateEditPoint();
point.EndOfLine();
var point2 = selection.BottomPoint.CreateEditPoint();
string line = point2.GetText(point);
if (line.Trim() == "throw new NotImplementedException();")
{
var delPoint = _delegateStart.CreateEditPoint();
delPoint.GetText(-1);
delPoint.WordLeft(3);
delPoint.Delete(_delegateStart);
_delegateStart.WordRight();
_delegateStart.Delete(1);
}
_delegateStart = null;
}
}
}