Skip to content

Commit

Permalink
only call NPPM_GETCURRENTLANGTYPE when necessary
Browse files Browse the repository at this point in the history
previously the DoInsertCloseHtmlTag method was
    wastefully calling NPPM_GETCURRENTLANGTYPE every
    time a char was added, even though a single call
    whenever a buffer opened or the lexer language changed
    would have sufficed.
This greatly improves the baseline performance of Notepad++
    while this plugin is installed.
  • Loading branch information
molsonkiko committed Feb 9, 2024
1 parent 69e5fdc commit 548e21f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Closing HTML/XML tags works inconsistently in Notepad++ v7.3.3.
- Avoid plugin crash when too-large int values are entered in the selection-remembering form.

## [0.0.3] - (UNRELEASED) YYYY-MM-DD

### Fixed

1. Greatly reduced unnecessary calls to `NPPM_GETCURRENTLANGTYPE` in `DoInsertHtmlCloseTag` by caching the lexer language whenever the lexer language changes or a buffer is opened.

## [0.0.2] - 2024-02-06

### Added
Expand Down
23 changes: 15 additions & 8 deletions NppCSharpPluginPack/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Main
public static Settings settings = new Settings();
public static bool bufferFinishedOpening;
public static string activeFname = null;
public static bool isDocTypeHTML = false;
// forms
public static SelectionRememberingForm selectionRememberingForm = null;
static internal int IdAboutForm = -1;
Expand Down Expand Up @@ -151,11 +152,16 @@ public static void OnNotification(ScNotification notification)
// This is usually unnecessary, but if there are multiple instances or multiple views,
// we need to track which of the currently visible buffers are actually being edited.
Npp.editor = new ScintillaGateway(PluginBase.GetCurrentScintilla());
DoesCurrentLexerSupportCloseHtmlTag();
// track when it was opened
IntPtr bufferOpenedId = notification.Header.IdFrom;
activeFname = Npp.notepad.GetFilePath(bufferOpenedId);
filesOpenedClosed.Add((activeFname, DateTime.Now, true));
return;
// when the lexer language changed, re-check whether this is a document where we close HTML tags.
case (uint)NppMsg.NPPN_LANGCHANGED:
DoesCurrentLexerSupportCloseHtmlTag();
break;
// when closing a file
case (uint)NppMsg.NPPN_FILEBEFORECLOSE:
IntPtr bufferClosedId = notification.Header.IdFrom;
Expand Down Expand Up @@ -320,18 +326,19 @@ static void CheckInsertHtmlCloseTag()
settings.SaveToIniFile();
}

static Regex regex = new Regex(@"[\._\-:\w]", RegexOptions.Compiled);

static internal void DoInsertHtmlCloseTag(char newChar)
static internal void DoesCurrentLexerSupportCloseHtmlTag()
{
LangType docType = LangType.L_TEXT;
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETCURRENTLANGTYPE, 0, ref docType);
bool isDocTypeHTML = (docType == LangType.L_HTML || docType == LangType.L_XML || docType == LangType.L_PHP);
isDocTypeHTML = (docType == LangType.L_HTML || docType == LangType.L_XML || docType == LangType.L_PHP);
}

static readonly Regex htmlTagNameRegex = new Regex(@"[\._\-:\w]", RegexOptions.Compiled);

if (!settings.close_html_tag || !isDocTypeHTML)
return;

if (newChar != '>')
static internal void DoInsertHtmlCloseTag(char newChar)
{
if (!(isDocTypeHTML && settings.close_html_tag && newChar == '>'))
return;

int bufCapacity = 512;
Expand Down Expand Up @@ -362,7 +369,7 @@ static internal void DoInsertHtmlCloseTag(char newChar)

var insertString = new StringBuilder("</");

while (regex.IsMatch(buf[pCur].ToString()))
while (htmlTagNameRegex.IsMatch(buf[pCur].ToString()))
{
insertString.Append(buf[pCur]);
pCur++;
Expand Down
4 changes: 2 additions & 2 deletions NppCSharpPluginPack/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("0.0.2.0")]
[assembly: AssemblyFileVersion("0.0.2.0")]
[assembly: AssemblyVersion("0.0.2.1")]
[assembly: AssemblyFileVersion("0.0.2.1")]

0 comments on commit 548e21f

Please sign in to comment.