forked from taqu/ForceLineFeedCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunningDocTableEvents.cs
224 lines (201 loc) · 8.67 KB
/
RunningDocTableEvents.cs
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.Linq;
namespace ForceLineFeedCode
{
internal class RunningDocTableEvents : IVsRunningDocTableEvents3
{
private ForceLineFeedCodePackage package_;
private RunningDocumentTable runningDocumentTable_;
/**
@brief Print a message to the editor's output
*/
[System.Diagnostics.Conditional("DEBUG")]
public static void output(string message, EnvDTE80.DTE2 dte)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
EnvDTE.OutputWindow outputWindow = dte.ToolWindows.OutputWindow;
if(null == outputWindow) {
return;
}
foreach(EnvDTE.OutputWindowPane window in outputWindow.OutputWindowPanes) {
window.OutputString(message);
}
}
public RunningDocTableEvents(ForceLineFeedCodePackage package)
{
package_ = package;
runningDocumentTable_ = new RunningDocumentTable(package);
runningDocumentTable_.Advise(this);
}
public int OnAfterAttributeChange(uint docCookie, uint grfAttribs)
{
return VSConstants.S_OK;
}
public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame)
{
return VSConstants.S_OK;
}
public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
{
return VSConstants.S_OK;
}
public int OnAfterSave(uint docCookie)
{
return VSConstants.S_OK;
}
public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
{
return VSConstants.S_OK;
}
public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
{
return VSConstants.S_OK;
}
public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarchy pHierOld, uint itemidOld, string pszMkDocumentOld, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew)
{
return VSConstants.S_OK;
}
/**
@brief Load a setting for the language
*/
private void loadSettings(out OptionPageForceLineFeedCode.TypeLineFeed linefeed, OptionPageForceLineFeedCode.TypeLanguage language, string documentPath)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
linefeed = OptionPageForceLineFeedCode.TypeLineFeed.LF;
//Load from a file
if (package_.Options.LoadSettingFile) {
SettingFile settingFile = package_.loadFileSettings(documentPath);
if(null != settingFile) {
linefeed = settingFile.get(language);
return;
}
}
//Otherwise, load from the option page
OptionPageForceLineFeedCode optionPage = package_.Options;
if (null != optionPage) {
switch (language) {
case OptionPageForceLineFeedCode.TypeLanguage.C_Cpp:
linefeed = optionPage.LineFeedCpp;
break;
case OptionPageForceLineFeedCode.TypeLanguage.CSharp:
linefeed = optionPage.LineFeedCSharp;
break;
default:
linefeed = optionPage.LineFeedOthers;
break;
}
}
}
public int OnBeforeSave(uint docCookie)
{
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
output(string.Format("Load _forcelinefeedcode.xml: {0}\n", package_.Options.LoadSettingFile), package_.DTE);
//Get the current document
RunningDocumentInfo runningDocumentInfo = runningDocumentTable_.GetDocumentInfo(docCookie);
EnvDTE.Document document = null;
foreach(EnvDTE.Document doc in package_.DTE.Documents.OfType<EnvDTE.Document>())
{
if(doc.FullName == runningDocumentInfo.Moniker)
{
document = doc;
break;
}
}
if(null == document) {
return VSConstants.S_OK;
}
if(document.Kind != "{8E7B96A8-E33D-11D0-A6D5-00C04FB67F6A}") {
return VSConstants.S_OK;
}
EnvDTE.TextDocument textDocument = document.Object("TextDocument") as EnvDTE.TextDocument;
if(null == textDocument) {
return VSConstants.S_OK;
}
//Get the current language
OptionPageForceLineFeedCode.TypeLanguage language = OptionPageForceLineFeedCode.TypeLanguage.Others;
switch (document.Language) {
case "C/C++":
language = OptionPageForceLineFeedCode.TypeLanguage.C_Cpp;
break;
case "CSharp":
language = OptionPageForceLineFeedCode.TypeLanguage.CSharp;
break;
default:
language = OptionPageForceLineFeedCode.TypeLanguage.Others;
break;
}
//Specify a target line-feed code
OptionPageForceLineFeedCode.TypeLineFeed linefeed = OptionPageForceLineFeedCode.TypeLineFeed.LF;
loadSettings(out linefeed, language, document.Path);
output(string.Format("doc:{0} => lang:{1} code:{2}\n", document.Language, language, linefeed), package_.DTE);
string replaceLineFeed;
switch(linefeed){
case OptionPageForceLineFeedCode.TypeLineFeed.LF:
replaceLineFeed = "\n";
break;
case OptionPageForceLineFeedCode.TypeLineFeed.CR:
replaceLineFeed = "\r";
break;
//case OptionPageForceLineFeedCode.TypeLineFeed.CRLF:
default:
replaceLineFeed = "\r\n";
break;
}
//Convert different codes
int count = 0;
EnvDTE.EditPoint editPoint = textDocument.StartPoint.CreateEditPoint();
if(OptionPageForceLineFeedCode.TypeLineFeed.CRLF == linefeed) {
while(!editPoint.AtEndOfDocument) {
editPoint.EndOfLine();
string lineending = editPoint.GetText(1);
if(string.IsNullOrEmpty(lineending)) {
continue;
}
for(int i = 0; i < lineending.Length; ++i) {
switch(lineending[i]) {
case '\n':
editPoint.ReplaceText(1, replaceLineFeed, 0);
++count;
break;
case '\r':
if((lineending.Length - 1)<=i) {
editPoint.ReplaceText(1, replaceLineFeed, 0);
++count;
}else if('\n' != lineending[i+1]) {
editPoint.ReplaceText(1, replaceLineFeed, 0);
++count;
}else {
++i;
}
break;
default:
break;
}
}
editPoint.CharRight();
}
}else {
while(!editPoint.AtEndOfDocument) {
editPoint.EndOfLine();
string lineending = editPoint.GetText(1);
if(string.IsNullOrEmpty(lineending)) {
continue;
}
for(int i = 0; i < lineending.Length; ++i) {
if(lineending[i] != replaceLineFeed[0]) {
editPoint.ReplaceText(1, replaceLineFeed, 0);
++count;
}
}
editPoint.CharRight();
}
}
output(string.Format("Replace {0} EOLs\n", count), package_.DTE);
return VSConstants.S_OK;
}
}
}