Skip to content

Commit 901b093

Browse files
committed
- Issue #64, #65
Added `TextRangeLegacy` to allow transparent hosting on otherwise incompatible versions of N++
1 parent 379686b commit 901b093

File tree

13 files changed

+108
-10
lines changed

13 files changed

+108
-10
lines changed

src/NppPlugin.Host/NppPlugin.Host.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<RootNamespace>NppPlugin.Host</RootNamespace>
1111
<AssemblyName>NppPlugin.Host</AssemblyName>
1212
<OutputPath>bin\Debug\</OutputPath>
13-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
1414
<FileUpgradeFlags>
1515
</FileUpgradeFlags>
1616
<UpgradeBackupLocation>
@@ -27,6 +27,7 @@
2727
<OutputPath>bin\x86\Debug\</OutputPath>
2828
<PlatformTarget>x86</PlatformTarget>
2929
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
30+
<Prefer32Bit>false</Prefer32Bit>
3031
</PropertyGroup>
3132
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3233
<DefineConstants>TRACE</DefineConstants>
@@ -36,6 +37,7 @@
3637
<ErrorReport>prompt</ErrorReport>
3738
<PlatformTarget>x86</PlatformTarget>
3839
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
40+
<Prefer32Bit>false</Prefer32Bit>
3941
</PropertyGroup>
4042
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
4143
<DebugSymbols>true</DebugSymbols>
@@ -46,6 +48,7 @@
4648
<PlatformTarget>x64</PlatformTarget>
4749
<ErrorReport>prompt</ErrorReport>
4850
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
51+
<Prefer32Bit>false</Prefer32Bit>
4952
</PropertyGroup>
5053
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
5154
<OutputPath>bin\x64\Release\</OutputPath>
@@ -56,6 +59,7 @@
5659
<PlatformTarget>x64</PlatformTarget>
5760
<ErrorReport>prompt</ErrorReport>
5861
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
62+
<Prefer32Bit>false</Prefer32Bit>
5963
</PropertyGroup>
6064
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
6165
<DebugSymbols>true</DebugSymbols>
@@ -66,6 +70,7 @@
6670
<PlatformTarget>x86</PlatformTarget>
6771
<ErrorReport>prompt</ErrorReport>
6872
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
73+
<Prefer32Bit>false</Prefer32Bit>
6974
</PropertyGroup>
7075
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
7176
<OutputPath>bin\x86\Release\</OutputPath>
@@ -76,6 +81,7 @@
7681
<PlatformTarget>x86</PlatformTarget>
7782
<ErrorReport>prompt</ErrorReport>
7883
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
84+
<Prefer32Bit>false</Prefer32Bit>
7985
</PropertyGroup>
8086
<ItemGroup>
8187
<Reference Include="System" />

src/NppPlugin.Host/PluginInfrastructure/GatewayDomain.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,18 @@ public CharacterRange(IntPtr cpmin, IntPtr cpmax)
223223
public IntPtr cpMax;
224224
}
225225

226+
[StructLayout(LayoutKind.Sequential)]
227+
public struct CharacterRangeLegacy
228+
{
229+
public CharacterRangeLegacy(int cpmin, int cpmax)
230+
{
231+
cpMin = cpmin; cpMax = cpmax;
232+
}
233+
234+
public int cpMin;
235+
public int cpMax;
236+
}
237+
226238
public class Cells
227239
{
228240
char[] charactersAndStyles;
@@ -235,6 +247,67 @@ public Cells(char[] charactersAndStyles)
235247
public char[] Value { get { return charactersAndStyles; } }
236248
}
237249

250+
public class TextRangeLegacy : IDisposable
251+
{
252+
Sci_TextRange _sciTextRange;
253+
IntPtr _ptrSciTextRange;
254+
bool _disposed = false;
255+
256+
public TextRangeLegacy(CharacterRangeLegacy chrRange, int stringCapacity)
257+
{
258+
_sciTextRange.chrg = chrRange;
259+
_sciTextRange.lpstrText = Marshal.AllocHGlobal(stringCapacity);
260+
}
261+
262+
public TextRangeLegacy(int cpmin, int cpmax, int stringCapacity)
263+
{
264+
_sciTextRange.chrg.cpMin = cpmin;
265+
_sciTextRange.chrg.cpMax = cpmax;
266+
_sciTextRange.lpstrText = Marshal.AllocHGlobal(stringCapacity);
267+
}
268+
269+
[StructLayout(LayoutKind.Sequential)]
270+
struct Sci_TextRange
271+
{
272+
public CharacterRangeLegacy chrg;
273+
public IntPtr lpstrText;
274+
}
275+
276+
public IntPtr NativePointer { get { _initNativeStruct(); return _ptrSciTextRange; } }
277+
278+
public string lpstrText { get { _readNativeStruct(); return Marshal.PtrToStringAnsi(_sciTextRange.lpstrText); } }
279+
280+
public CharacterRangeLegacy chrg { get { _readNativeStruct(); return _sciTextRange.chrg; } set { _sciTextRange.chrg = value; _initNativeStruct(); } }
281+
282+
void _initNativeStruct()
283+
{
284+
if (_ptrSciTextRange == IntPtr.Zero)
285+
_ptrSciTextRange = Marshal.AllocHGlobal(Marshal.SizeOf(_sciTextRange));
286+
Marshal.StructureToPtr(_sciTextRange, _ptrSciTextRange, false);
287+
}
288+
289+
void _readNativeStruct()
290+
{
291+
if (_ptrSciTextRange != IntPtr.Zero)
292+
_sciTextRange = (Sci_TextRange)Marshal.PtrToStructure(_ptrSciTextRange, typeof(Sci_TextRange));
293+
}
294+
295+
public void Dispose()
296+
{
297+
if (!_disposed)
298+
{
299+
if (_sciTextRange.lpstrText != IntPtr.Zero) Marshal.FreeHGlobal(_sciTextRange.lpstrText);
300+
if (_ptrSciTextRange != IntPtr.Zero) Marshal.FreeHGlobal(_ptrSciTextRange);
301+
_disposed = true;
302+
}
303+
}
304+
305+
~TextRangeLegacy()
306+
{
307+
Dispose();
308+
}
309+
}
310+
238311
public class TextRange : IDisposable
239312
{
240313
Sci_TextRange _sciTextRange;

src/NppPlugin.Host/PluginInfrastructure/IScintillaGateway.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,8 @@ public interface IScintillaGateway
806806
/// </summary>
807807
int GetTextRange(TextRange tr);
808808

809+
int GetTextRangeLegacy(TextRangeLegacy tr);
810+
809811
/// <summary>Draw the selection in normal style or with selection highlighted. (Scintilla feature 2163)</summary>
810812
void HideSelection(bool normal);
811813

src/NppPlugin.Host/PluginInfrastructure/NppPluginNETBase.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ public partial class PluginBase
3131
public static NppData nppData;
3232
public static FuncItems _funcItems = new FuncItems();
3333

34+
static public int[] GetNppVersion()
35+
{
36+
IntPtr version = Win32.SendMessage(Editor.Handle, (uint)NppMsg.NPPM_GETNPPVERSION, 0, 0);
37+
38+
int major = unchecked((short)((long)version >> 16));
39+
int minor = unchecked((short)(long)version);
40+
41+
return new[] { major, minor };
42+
}
43+
3444
public static void SetCommand(int index, string commandName, NppFuncItemDelegate functionPointer)
3545
{
3646
SetCommand(index, commandName, functionPointer, new ShortcutKey(), false);

src/NppPlugin.Host/PluginInfrastructure/ScintillaGateway.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,12 @@ public int GetTextRange(TextRange tr)
16521652
return (int)res;
16531653
}
16541654

1655+
public int GetTextRangeLegacy(TextRangeLegacy tr)
1656+
{
1657+
IntPtr res = Win32.SendMessage(scintilla, SciMsg.SCI_GETTEXTRANGE, Unused, tr.NativePointer);
1658+
return (int)res;
1659+
}
1660+
16551661
/// <summary>Draw the selection in normal style or with selection highlighted. (Scintilla feature 2163)</summary>
16561662
public void HideSelection(bool normal)
16571663
{

src/NppPlugin.Host/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.2.0")]
36-
[assembly: AssemblyFileVersion("1.0.2.0")]
35+
[assembly: AssemblyVersion("1.0.3.0")]
36+
[assembly: AssemblyFileVersion("1.0.3.0")]

src/NppPlugin.Host/Properties/Resources.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
1 KB
Binary file not shown.
1 KB
Binary file not shown.
1 KB
Binary file not shown.

0 commit comments

Comments
 (0)