Skip to content

Commit

Permalink
Provide API wrappers for document text retrieval
Browse files Browse the repository at this point in the history
Also:

 * hide `NotepadPPGateway.GetString`
 * de-duplicate API documentation for readability
  • Loading branch information
rdipardo committed Feb 2, 2025
1 parent 6d7300c commit 0a8ed8d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/Npp.DotNet.Plugin/Kbg.NppPluginNET/Msgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ static class Constants
public const int CONT_BOTTOM = 3;
public const int DOCKCONT_MAX = 4;
public const int MENU_TITLE_LENGTH = 64;
/// <summary>
/// Defined in <c>PowerEditor/src/WinControls/StaticDialog/RunDlg/RunDlg.h</c>
/// </summary>
public const int CURRENTWORD_MAXLENGTH = 0x800;
}

public enum LangType
Expand Down Expand Up @@ -195,7 +199,6 @@ public enum NppMsg : uint
/// </summary>
/// <returns>TRUE</returns>
NPPM_DESTROYSCINTILLAHANDLE_DEPRECATED = NPPMSG + 21,
/// <inheritdoc cref="NPPM_DESTROYSCINTILLAHANDLE_DEPRECATED"/>
[Obsolete("Replaced by NPPM_DESTROYSCINTILLAHANDLE_DEPRECATED", true)]
NPPM_DESTROYSCINTILLAHANDLE = NPPM_DESTROYSCINTILLAHANDLE_DEPRECATED,
/// <summary>
Expand Down Expand Up @@ -381,7 +384,6 @@ public enum NppMsg : uint
/// </summary>
/// <returns>TRUE</returns>
NPPM_ADDTOOLBARICON_DEPRECATED = NPPMSG + 41,
/// <inheritdoc cref="NPPM_ADDTOOLBARICON_DEPRECATED"/>
[Obsolete("Replaced by NPPM_ADDTOOLBARICON_DEPRECATED", true)]
NPPM_ADDTOOLBARICON = NPPM_ADDTOOLBARICON_DEPRECATED,
/// <summary>
Expand Down Expand Up @@ -715,7 +717,6 @@ public enum NppMsg : uint
/// </summary>
/// <returns>TRUE if NPPM_ALLOCATECMDID is supported</returns>
NPPM_ALLOCATESUPPORTED_DEPRECATED = NPPMSG + 80,
/// <inheritdoc cref="NPPM_ALLOCATESUPPORTED_DEPRECATED"/>
[Obsolete("Replaced by NPPM_ALLOCATESUPPORTED_DEPRECATED", true)]
NPPM_ALLOCATESUPPORTED = NPPM_ALLOCATESUPPORTED_DEPRECATED,
/// <summary>
Expand Down
42 changes: 41 additions & 1 deletion lib/Npp.DotNet.Plugin/Kbg.NppPluginNET/NotepadPPGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public interface INotepadPPGateway
string GetPluginsHomePath();
string GetPluginConfigPath();
string GetSessionFilePath();
string GetCurrentWord();
string GetCurrentLine();
string GetCurrentFilePath();
string GetFilePath(UIntPtr bufferId);
string GetNativeLanguage();
Expand Down Expand Up @@ -101,6 +103,20 @@ public void AddToolbarIcon(int funcItemsIndex, Bitmap icon)
}
#pragma warning restore CS0618

/// <summary>
/// Gets the word currently under the caret.
/// </summary>
/// <returns>The word under the caret.</returns>
public string GetCurrentWord()
=> GetDocumentString(NppMsg.NPPM_GETCURRENTWORD);

/// <summary>
/// Gets the text of the currently active line.
/// </summary>
/// <returns>The text of the active line.</returns>
public string GetCurrentLine()
=> GetDocumentString(NppMsg.NPPM_GETCURRENTLINESTR);

/// <summary>
/// Gets the path of the current document.
/// </summary>
Expand All @@ -109,6 +125,30 @@ public string GetCurrentFilePath()
return NppUtils.GetCurrentPath(NppUtils.PathType.FULL_CURRENT_PATH);
}

/// <summary>
/// Gets a string of text in the code page of the current document.
/// </summary>
/// <param name="message">The <see cref="NppMsg"/> to be sent.</param>
/// <returns>A string of document text, or <see cref="string.Empty"/>.</returns>
static string GetDocumentString(NppMsg message)
{
var sci = new ScintillaGateway(Utils.GetCurrentScintilla());
bool isUnicode = sci.GetCodePage() == (int)SciMsg.SC_CP_UTF8;
string buffer = new string('\0', Constants.CURRENTWORD_MAXLENGTH);
IntPtr pBuf = isUnicode ? Marshal.StringToHGlobalUni(buffer) : Marshal.StringToHGlobalAnsi(buffer);
string result = string.Empty;
try
{
if (Win32.TRUE == (NativeBool)Win32.SendMessage(PluginData.NppData.NppHandle, (uint)message, (uint)buffer.Length, pBuf))
result = isUnicode ? Marshal.PtrToStringUni(pBuf) : Marshal.PtrToStringAnsi(pBuf);
}
finally
{
Marshal.FreeHGlobal(pBuf);
}
return result;
}

/// <summary>
/// This method encapsulates a common pattern in the Notepad++ API: when
/// you need to retrieve a string, you can first query the buffer size.
Expand All @@ -121,7 +161,7 @@ public string GetCurrentFilePath()
/// <see langword="false"/> if the out parameter is a byte buffer (<c>char*</c>).
/// </param>
/// <returns>String returned by Notepad++.</returns>
public static string GetString(NppMsg message, bool returnsWideString = true)
static string GetString(NppMsg message, bool returnsWideString = true)
{
int len = Win32.SendMessage(
PluginData.NppData.NppHandle,
Expand Down

0 comments on commit 0a8ed8d

Please sign in to comment.