How to create a C# plugin?
-
I’m trying to create a plugin for Notepad++ in order to integrate NLog (like line colouring, filtering, …).
For that I’ve tried to get the so-called “Demo” plugin working, but I’m having lots of troubles (see StackOverflow post “https://stackoverflow.com/questions/79632308”).
Is there somewhere a working C# plugin (together with a working “DllExport”) that I can use as a starting point?
Thanks in advance
Dominique -
@scampsd ,
Please be patient, and do not post two copies of the same question. A message similar to the following, when you were creating your post(s), should have made it clear that you would have to wait until your post was approved:
Is there somewhere a working C# plugin (together with a working “DllExport”) that I can use as a starting point?
Have you tried looking in the Online User Manual at https://npp-user-manual.org/, where the Plugins page has a section on plugin templates for various languages, which includes two actively-maintained C# plugin templates?
kbilsted’s NotepadPlusPlusPluginPack.Net, mentioned in the SO discussion you linked to, hadn’t been updated for years (which is why it was recently removed from the User Manual list of templates): @Mark-Olson’s NppCSharpPluginPack mentioned in the Manual is a fork of that template, and has been updated to work with modern Notpead++ (and he’s a regular here and actively maintaining that template), so he’ll probably chime in with more advice for you.
-
@scampsd
I would recommend NppCSharpPluginPack, which @PeterJones referenced above. I maintain it, and I don’t know of any other C# plugin templates that are actively maintained. -
@Mark-Olson
Let me start reacting with one word: WOW!!!The Plugin template you provide is indeed what I’m looking for.
Let me also explain what I’m aiming at:
In my company were are using NLog as a logging library and all of our logs have a similar look, as can be seen in the following example:2025-02-10 00:02:54.9425 | Debug | Namespace.InternalClass(.Method) | Some information
- Starting with a timestamp with a fix format
- Followed by the loglevel
- After that, there is “namespace”, “class”, sometimes there is also “method”
- The last part is the information, which is to be logged.
- The separation between the logging fields is always “<space><pipe><space>”.
I would like to create a plugin, which can colour the background of the logs according to loglevel. In top of that, I might develop a filtering mechanism, based on timestamp. (This is just a start)
In order to do that, I would like to program the following in my plugin:
- An event, which is triggered when a line is shown in Notepad++. When that line contains “| Debug |”, the line needs to be coloured (background colour and font colour, if possible). Is it possible subscribing to such an event and how?
- A form, where those things can be configured. As I have some background in XAML, this might be interesting. Does the plugin development engine provide XAML support?
Thanks in advance
-
-
Dear,
As you can see from my question, I was thinking in the sense of creating an event and handling accordingly, but the plugin you provided, seems to be based on a completely different way of working. As an example, let me show you a line of code from the function, which enables/disables the background colouring:Win32.SendMessage( PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_SETCURRENTLANGTYPE, 0, newLanguageId);
For a Notepad++ plugin newbie like me, this is really not simple. Can you shed some light on what this all means? (How does the mentioned line of source code modify the background of a piece of a line in Notepad++?)
Thanks
Dominique -
NPPM_SETCURRENTLANGTYPE is an NPP message that asks Npp to set a specific lexer and the code of the lexer is ultimately responsible for coloring the code. Your plugin can, but does not have to, define a lexer, this is how csvlint has solved this and is usually the better solution. Another approach would be to search for the desired lines and color them accordingly. I can’t say what makes more sense for you, as I don’t know what kind of data you are working with or what your knowledge of lexers, styles etc… is like.
Regarding the events, Npp calls a callback, beNotified, which is exported by plugins. Using the C# template this is the OnNotification in Main.cs. The plugin can use this to react to various events.
EDIT: Here the link to the scintilla documentation. -
@scampsd said in How to create a C# plugin?:
A form, where those things can be configured. As I have some background in XAML, this might be interesting. Does the plugin development engine provide XAML support?
XAML is not supported by NppCSharpPluginPack. I know nothing about XAML, so I couldn’t comment on how easy it would be to modify the template to support that.
I use Windows Forms to make forms in plugins based on that template. See the
NppDemo.Forms
namespace for how I make forms.