-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.cs
420 lines (333 loc) · 13.7 KB
/
Main.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Kbg.NppPluginNET.PluginInfrastructure;
namespace Kbg.NppPluginNET
{
class Main
{
internal const string PluginName = "JSFunctionViewer";
static string iniFilePath = null;
//static bool someSetting = false;
static frmMyDlg frmFuncView = null;
static int idFuncView = -1;
public static bool isFuncEnabled = false;
static Bitmap tbBmp = new Bitmap(JSFunctionViewer.Properties.Resources.JSFunctionViewer);
static Bitmap tbBmp_tbTab = new Bitmap(JSFunctionViewer.Properties.Resources.JSFunctionViewer_bmp);
static Icon tbIcon = null;
static bool isShuttingDown = false;
static string externalFilePath = "";
static bool isShown = false;
public static void OnNotification(ScNotification notification)
{
if (notification.Header.Code == (uint)NppMsg.NPPN_BEFORESHUTDOWN || notification.Header.Code == (uint)NppMsg.NPPN_SHUTDOWN)
{
isShuttingDown = true;
}
if (!isShuttingDown && notification.Header.Code != (uint)NppMsg.NPPN_FILEBEFORELOAD)
{
RefreshFunction();
}
}
internal static void CommandMenuInit()
{
StringBuilder sbIniFilePath = new StringBuilder(Win32.MAX_PATH);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbIniFilePath);
iniFilePath = sbIniFilePath.ToString();
if (!Directory.Exists(iniFilePath)) Directory.CreateDirectory(iniFilePath);
iniFilePath = Path.Combine(iniFilePath, PluginName + ".ini");
//someSetting = (Win32.GetPrivateProfileInt("SomeSection", "SomeKey", 0, iniFilePath) != 0);
PluginBase.SetCommand(0, "Toggle Function Viewer", FunctionViewer/*, new ShortcutKey(false, true, false, Keys.B)*/);
idFuncView = 0;
}
internal static Bitmap GetCurrenctIcon() //Please tell me if there's a way to re-set an icon, 'till then, this function'll be sad
{
if (isFuncEnabled)
{
return new Bitmap(JSFunctionViewer.Properties.Resources.JSFunctionViewer);
}
else
{
return new Bitmap(JSFunctionViewer.Properties.Resources.JSFunctionViewer_Gray);
}
}
internal static void SetToolBarIcon()
{
//tbBmp = GetCurrenctIcon(); :(
toolbarIcons tbIcons = new toolbarIcons();
tbIcons.hToolbarBmp = tbBmp.GetHbitmap();
IntPtr pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(tbIcons));
Marshal.StructureToPtr(tbIcons, pTbIcons, false);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_ADDTOOLBARICON, PluginBase._funcItems.Items[idFuncView]._cmdID, pTbIcons); //todo: dark mode
Marshal.FreeHGlobal(pTbIcons);
}
internal static void PluginCleanUp()
{
//Win32.WritePrivateProfileString("SomeSection", "SomeKey", someSetting ? "1" : "0", iniFilePath);
}
internal static void FunctionViewer()
{
if (frmFuncView == null)
{
frmFuncView = new frmMyDlg();
using (Bitmap newBmp = new Bitmap(16, 16))
{
Graphics g = Graphics.FromImage(newBmp);
ColorMap[] colorMap = new ColorMap[1];
colorMap[0] = new ColorMap();
colorMap[0].OldColor = Color.Fuchsia;
colorMap[0].NewColor = Color.FromKnownColor(KnownColor.ButtonFace);
ImageAttributes attr = new ImageAttributes();
attr.SetRemapTable(colorMap);
g.DrawImage(tbBmp_tbTab, new Rectangle(0, 0, 16, 16), 0, 0, 16, 16, GraphicsUnit.Pixel, attr);
tbIcon = Icon.FromHandle(newBmp.GetHicon());
}
NppTbData _nppTbData = new NppTbData();
_nppTbData.hClient = frmFuncView.Handle;
_nppTbData.pszName = "Function Viewer";
_nppTbData.dlgID = idFuncView;
_nppTbData.uMask = NppTbMsg.DWS_DF_CONT_RIGHT | NppTbMsg.DWS_ICONTAB | NppTbMsg.DWS_ICONBAR;
_nppTbData.hIconTab = (uint)tbIcon.Handle;
_nppTbData.pszModuleName = PluginName;
IntPtr _ptrNppTbData = Marshal.AllocHGlobal(Marshal.SizeOf(_nppTbData));
Marshal.StructureToPtr(_nppTbData, _ptrNppTbData, false);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_DMMREGASDCKDLG, 0, _ptrNppTbData);
isFuncEnabled = true;
}
else
{
isFuncEnabled = !isFuncEnabled;
}
}
public static void ShowDialog()
{
if (!isShown)
{
isShown = true;
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_DMMSHOW, 0, frmFuncView.Handle);
}
}
public static void HideDialog()
{
if (!frmFuncView.CheckStayCheckBox() || !isFuncEnabled)
{
isShown = false;
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_DMMHIDE, 0, frmFuncView.Handle);
}
}
internal static string ReadFile(string path)
{
string result;
using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
{
result = streamReader.ReadToEnd();
}
return result;
}
internal static string GetSelectedText()
{
int length = (int)Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETSELTEXT, 0, 0);
IntPtr ptrToText = Marshal.AllocHGlobal(length + 1);
Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETSELTEXT, length, ptrToText);
return Marshal.PtrToStringAnsi(ptrToText);
}
internal static string GetCurrentFilePath()
{
StringBuilder filePath = new StringBuilder(Win32.MAX_PATH);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETFULLCURRENTPATH, Win32.MAX_PATH, filePath);
return filePath.ToString();
}
internal static string TrimFunction(string text)
{
int layers = 0;
for (int i = 9; i < text.Length; i++)
{
if (text[i] == '{')
{
layers++;
}
else if (text[i] == '}')
{
layers--;
if (layers == 0)
{
text = text.Substring(0, i + 1);
break;
}
}
}
return text;
}
internal static List<char> GetIndents(string[] lines){
List<char> indents = new List<char>();
for (int i = 0; i < lines[lines.Length - 1].Length; i++)
{
char indent = lines[lines.Length - 1][i];
if (indent == ' ' || indent == '\t')
{
indents.Add(indent);
}
else
{
break;
}
}
return indents;
}
internal static string RemoveExtraIndents(string[] lines, List<char> indents)
{
for (int i = 1; i < lines.Length; i++)
{
int indentsNum = 0;
for (int j = 0; j < indents.Count; j++)
{
if (lines[i].Length > j) {
if (lines[i][j] == indents[j])
{
indentsNum++;
}
else
{
break;
}
}
}
lines[i] = lines[i].Substring(indentsNum);
}
return String.Join(Environment.NewLine, lines);
}
internal static string FixFunction(string text)
{
string[] lines = text.Split(
new string[] { Environment.NewLine },
StringSplitOptions.None
);
List<char> indents = GetIndents(lines);
return RemoveExtraIndents(lines, indents);
}
internal static List<string> GetExternalFiles(string currentFilePath)
{
List<string> externalFiles = new List<string>();
string currentFile = ReadFile(currentFilePath);
string[] sources = currentFile.Split(
new string[] { "src=\"" },
StringSplitOptions.None
);
for (int i = 1; i < sources.Length; i++)
{
int index = sources[i].IndexOf("\"");
if (sources[i].Substring(index + 1, 10) == "></script>")
{
externalFiles.Add(sources[i].Substring(0, index));
}
}
return externalFiles;
}
internal static void AddExternalFiles(List<string> list)
{
int lastSlash = list[0].LastIndexOf('\\');
string folder = (lastSlash > -1) ? list[0].Substring(0, lastSlash + 1) : "";
List<string> externalFiles = GetExternalFiles(list[0]);
for (int i = 0; i < externalFiles.Count; i++)
{
externalFiles[i] = Regex.Replace(externalFiles[i], @"[\/]", "\\");
if (externalFiles[i][1] == ':')
{
list.Add(externalFiles[i]);
}
else
{
list.Add(folder + externalFiles[i]);
}
}
}
internal static void CheckGoToButtons()
{
isShown = false;
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_DMMHIDE, 0, frmFuncView.Handle);
if (frmFuncView.CheckGoToFunction())
{
string selectedText = GetSelectedText();
string functionName = "function " + selectedText + "(";
Clipboard.SetText(functionName);
if (externalFilePath != "")
{
Process.Start("notepad++.exe", "\"" + externalFilePath + "\"");
}
//HideDialog();
SendKeys.SendWait("%(S{ENTER})^(V){ENTER}{ESCAPE}");
}
else if (frmFuncView.CheckGoUp())
{
//HideDialog();
SendKeys.SendWait("%(SSS{ENTER})");
}
else if (frmFuncView.CheckGoDown())
{
//HideDialog();
SendKeys.SendWait("%(SS{ENTER})");
}
}
internal static void RefreshFunction()
{
if (isFuncEnabled)
{
string selectedText = GetSelectedText();
string functionName = "function " + selectedText + "(";
List<string> filePaths = new List<string>();
filePaths.Add(GetCurrentFilePath());
AddExternalFiles(filePaths);
bool isActive = false;
for (int i = 0; i < filePaths.Count; i++)
{
bool isReal = true;
try
{
ReadFile(filePaths[i]);
}
catch
{
isReal = false;
}
if (isReal)
{
string fileContents = ReadFile(filePaths[i]);
if (fileContents.Contains(functionName))
{
string functionText = fileContents.Substring(fileContents.IndexOf(functionName));
functionText = TrimFunction(functionText);
functionText = FixFunction(functionText);
if (i > 0)
{
frmFuncView.HideGoUpDownButtons();
externalFilePath = filePaths[i];
functionText = Environment.NewLine + " (From: " + filePaths[i] + ")" + Environment.NewLine + Environment.NewLine + functionText;
}
else
{
frmFuncView.ShowGoUpDownButtons();
externalFilePath = "";
functionText = Environment.NewLine + Environment.NewLine + Environment.NewLine + functionText;
}
frmFuncView.ChangeText(functionText);
ShowDialog();
isActive = true;
break;
}
}
}
if (!isActive)
{
HideDialog();
}
}
}
}
}