Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit b7f2e73

Browse files
committed
code cleanup
1 parent 568ee41 commit b7f2e73

File tree

1 file changed

+48
-49
lines changed
  • Demo Plugin/NppManagedPluginDemo

1 file changed

+48
-49
lines changed

Demo Plugin/NppManagedPluginDemo/Demo.cs

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ static internal void SetToolBarIcon()
3333
Kbg.Demo.Namespace.Main.SetToolBarIcon();
3434
}
3535

36-
static internal void OnCharAdded(char newChar)
37-
{
38-
}
39-
4036
public static void OnNotification(ScNotification notification)
4137
{
4238
if (notification.Header.code == (uint)SciMsg.SCN_CHARADDED)
@@ -264,59 +260,62 @@ static void checkInsertHtmlCloseTag()
264260
int i = Win32.CheckMenuItem(Win32.GetMenu(PluginBase.nppData._nppHandle), PluginBase._funcItems.Items[9]._cmdID,
265261
Win32.MF_BYCOMMAND | (doCloseTag ? Win32.MF_CHECKED : Win32.MF_UNCHECKED));
266262
}
263+
264+
static Regex regex = new Regex(@"[\._\-:\w]", RegexOptions.Compiled);
265+
267266
static internal void doInsertHtmlCloseTag(char newChar)
268267
{
269268
LangType docType = LangType.L_TEXT;
270269
Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETCURRENTLANGTYPE, 0, ref docType);
271270
bool isDocTypeHTML = (docType == LangType.L_HTML || docType == LangType.L_XML || docType == LangType.L_PHP);
272-
if (doCloseTag && isDocTypeHTML)
271+
272+
if (!doCloseTag || !isDocTypeHTML)
273+
return;
274+
275+
if (newChar != '>')
276+
return;
277+
278+
int bufCapacity = 512;
279+
var pos = editor.GetCurrentPos();
280+
int currentPos = pos.Value;
281+
int beginPos = currentPos - (bufCapacity - 1);
282+
int startPos = (beginPos > 0) ? beginPos : 0;
283+
int size = currentPos - startPos;
284+
285+
if (size < 3)
286+
return;
287+
288+
using (TextRange tr = new TextRange(startPos, currentPos, bufCapacity))
273289
{
274-
if (newChar == '>')
290+
editor.GetTextRange(tr);
291+
string buf = tr.lpstrText;
292+
293+
if (buf[size - 2] == '/')
294+
return;
295+
296+
int pCur = size - 2;
297+
while ((pCur > 0) && (buf[pCur] != '<') && (buf[pCur] != '>'))
298+
pCur--;
299+
300+
if (buf[pCur] == '<')
275301
{
276-
int bufCapacity = 512;
277-
var pos = editor.GetCurrentPos();
278-
int currentPos = pos.Value;
279-
int beginPos = currentPos - (bufCapacity - 1);
280-
int startPos = (beginPos > 0) ? beginPos : 0;
281-
int size = currentPos - startPos;
282-
283-
if (size >= 3)
302+
pCur++;
303+
304+
var insertString = new StringBuilder("</");
305+
306+
while (regex.IsMatch(buf[pCur].ToString()))
307+
{
308+
insertString.Append(buf[pCur]);
309+
pCur++;
310+
}
311+
insertString.Append('>');
312+
313+
if (insertString.Length > 3)
284314
{
285-
using (TextRange tr = new TextRange(startPos, currentPos, bufCapacity))
286-
{
287-
editor.GetTextRange(tr);
288-
string buf = tr.lpstrText;
289-
290-
if (buf[size - 2] != '/')
291-
{
292-
StringBuilder insertString = new StringBuilder("</");
293-
294-
int pCur = size - 2;
295-
for (; (pCur > 0) && (buf[pCur] != '<') && (buf[pCur] != '>'); )
296-
pCur--;
297-
298-
if (buf[pCur] == '<')
299-
{
300-
pCur++;
301-
302-
Regex regex = new Regex(@"[\._\-:\w]");
303-
while (regex.IsMatch(buf[pCur].ToString()))
304-
{
305-
insertString.Append(buf[pCur]);
306-
pCur++;
307-
}
308-
insertString.Append('>');
309-
310-
if (insertString.Length > 3)
311-
{
312-
editor.BeginUndoAction();
313-
editor.ReplaceSel(insertString.ToString());
314-
editor.SetSel(pos, pos);
315-
editor.EndUndoAction();
316-
}
317-
}
318-
}
319-
}
315+
editor.BeginUndoAction();
316+
editor.ReplaceSel(insertString.ToString());
317+
editor.SetSel(pos, pos);
318+
editor.EndUndoAction();
320319
}
321320
}
322321
}

0 commit comments

Comments
 (0)