Skip to content

Commit

Permalink
plugin now compatible with Npp older than 8.4.1
Browse files Browse the repository at this point in the history
FIXED
1. Fixed bug in [Main](/JsonToolsNppPlugin/Main.cs#L360) (based on failure to read [SCI_GETTEXT documentation](https://www.scintilla.org/ScintillaDoc.html#SCI_GETTEXT)) that caused this plugin to be incompatible with versions of Notepad++ older than 8.4.1.

ADDED
1. DSON emitter and UDL takes this plugin to the moon!
  • Loading branch information
molsonkiko committed Feb 28, 2023
1 parent a28eeeb commit e2ffde3
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 8 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

1. Hopefully eliminated crash bug that sometimes seems to happen because a file that had a tree viewer associated with it was renamed. This bug is really unpredictable, so it may not be gone.
2. Changed [RemesPath `s_sub` function](/docs/RemesPath.md#vectorized-functions) so that it either does regex-replace or simple string-replace, depending on the type of the second parameter.
2. Fixed bug in [Main](/JsonToolsNppPlugin/Main.cs#L360) (based on failure to read [SCI_GETTEXT documentation](https://www.scintilla.org/ScintillaDoc.html#SCI_GETTEXT)) that caused this plugin to be incompatible with versions of Notepad++ older than 8.4.1.

### Changed
1. Changed [RemesPath `s_sub` function](/docs/RemesPath.md#vectorized-functions) so that it either does regex-replace or simple string-replace, depending on the type of the second parameter.

#### Added
1. [DSON emitter and UDL](/docs/README.md#dson) takes this plugin to the moon!

## [4.10.0] - 2023-02-15

Expand Down
64 changes: 64 additions & 0 deletions DSON UDL.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<NotepadPlus>
<UserLang name="DSON" ext="dson" udlVersion="2.1">
<Settings>
<Global caseIgnored="no" allowFoldOfComments="no" foldCompact="no" forcePureLC="0" decimalSeparator="0" />
<Prefix Keywords1="yes" Keywords2="yes" Keywords3="no" Keywords4="no" Keywords5="no" Keywords6="no" Keywords7="no" Keywords8="no" />
</Settings>
<KeywordLists>
<Keywords name="Comments">00 01 02 03 04</Keywords>
<Keywords name="Numbers, prefix1">+ -</Keywords>
<Keywords name="Numbers, prefix2"></Keywords>
<Keywords name="Numbers, extras1"></Keywords>
<Keywords name="Numbers, extras2"></Keywords>
<Keywords name="Numbers, suffix1"></Keywords>
<Keywords name="Numbers, suffix2"></Keywords>
<Keywords name="Numbers, range">VERY very</Keywords>
<Keywords name="Operators1">, ? . !</Keywords>
<Keywords name="Operators2"></Keywords>
<Keywords name="Folders in code1, open"></Keywords>
<Keywords name="Folders in code1, middle"></Keywords>
<Keywords name="Folders in code1, close"></Keywords>
<Keywords name="Folders in code2, open"></Keywords>
<Keywords name="Folders in code2, middle"></Keywords>
<Keywords name="Folders in code2, close"></Keywords>
<Keywords name="Folders in comment, open"></Keywords>
<Keywords name="Folders in comment, middle"></Keywords>
<Keywords name="Folders in comment, close"></Keywords>
<Keywords name="Keywords1">so and also many</Keywords>
<Keywords name="Keywords2">such is wow</Keywords>
<Keywords name="Keywords3">yes no</Keywords>
<Keywords name="Keywords4">empty</Keywords>
<Keywords name="Keywords5"></Keywords>
<Keywords name="Keywords6"></Keywords>
<Keywords name="Keywords7"></Keywords>
<Keywords name="Keywords8"></Keywords>
<Keywords name="Delimiters">00&quot; 01\ 02&quot; 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23</Keywords>
</KeywordLists>
<Styles>
<WordsStyle name="DEFAULT" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="COMMENTS" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="LINE COMMENTS" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="NUMBERS" fgColor="FF3737" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS1" fgColor="00FF80" bgColor="FFFFFF" fontStyle="1" nesting="0" />
<WordsStyle name="KEYWORDS2" fgColor="FF00FF" bgColor="FFFFFF" fontStyle="1" nesting="0" />
<WordsStyle name="KEYWORDS3" fgColor="0000FF" bgColor="FFFFFF" fontStyle="1" nesting="0" />
<WordsStyle name="KEYWORDS4" fgColor="000000" bgColor="FFFFFF" fontStyle="1" nesting="0" />
<WordsStyle name="KEYWORDS5" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS6" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS7" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="KEYWORDS8" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="OPERATORS" fgColor="008080" bgColor="FFFFFF" fontStyle="2" nesting="0" />
<WordsStyle name="FOLDER IN CODE1" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="FOLDER IN CODE2" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="FOLDER IN COMMENT" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS1" fgColor="808000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS2" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS3" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS4" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS5" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS6" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS7" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
<WordsStyle name="DELIMITERS8" fgColor="000000" bgColor="FFFFFF" fontStyle="0" nesting="0" />
</Styles>
</UserLang>
</NotepadPlus>
130 changes: 130 additions & 0 deletions JsonToolsNppPlugin/JSONTools/Dson.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;

namespace JSON_Tools.JSON_Tools
{
class DsonDumpException : Exception
{
public new string Message;

public DsonDumpException(string message)
{
Message = message;
}
}

/// <summary>
/// wow such data very readable<br></br>
/// such docs at https://dogeon.xyz/ plz read
/// </summary>
class Dson
{
public static string KeyValuePairDelims = ",.!?";

public static string[] ArrayPairDelims = new string[] { "and", "also" };

private static string FormatInteger(long val)
{
if (val == long.MinValue)
// converting to octal turns signed numbers into unsigned numbers
// this can be reversed for every negative number except long.MinValue
return "1000000000000000000000";
if (val < 0)
return "-" + Convert.ToString(-val, 8);
return Convert.ToString(val, 8);
}

/// <summary>
/// plz stringify json as DSON<br></br>
/// wow so amaze
/// </summary>
public static string Dump(JNode json)
{
StringBuilder sb;
switch (json.type)
{
case Dtype.ARR:
sb = new StringBuilder();
sb.Append("so ");
JArray arr = (JArray)json;
bool useAndDelim = true;
for (int ii = 0; ii < arr.Length; ii++)
{
sb.Append(Dump(arr[ii]));
sb.Append(useAndDelim ? " and " : " also ");
useAndDelim = !useAndDelim;
}
if (arr.Length > 0)
{
// trim off the last delimiter if the array had items
int lengthToRemove = useAndDelim ? 5 : 4;
sb.Remove(sb.Length - lengthToRemove - 1, lengthToRemove);
}
sb.Append("many");
return sb.ToString();
case Dtype.OBJ:
// {"a": 13.51e25, "b": true} becomes
// such "a" is 15.63very31, "b" is yes wow
sb = new StringBuilder();
sb.Append("such ");
int delimIdx = 0;
JObject obj = (JObject)json;
foreach (string key in obj.children.Keys)
{
JNode value = obj[key];
sb.Append($"\"{key}\" is ");
sb.Append(Dump(value));
sb.Append(KeyValuePairDelims[delimIdx % 4] + " ");
delimIdx++;
}
if (obj.Length > 0)
sb.Remove(sb.Length - 2, 1); // remove the last delimter if there were items
sb.Append("wow");
return sb.ToString();
case Dtype.BOOL:
return (bool)json.value ? "yes" : "no";
case Dtype.NULL:
return "empty";
case Dtype.INT:
return FormatInteger((long)json.value);
case Dtype.FLOAT:
// floating point numbers are formatted
// such that fractional part, exponent, and integer part are all octal
string valstr = ((double)json.value).ToString();
if (valstr.EndsWith("y") || valstr.EndsWith("N"))
// Infinity and NaN are not in the DSON specification
throw new DsonDumpException($"{valstr} is fake number, can't understand. So silly, wow");
StringBuilder partSb = new StringBuilder();
sb = new StringBuilder();
long part;
foreach (char c in valstr)
{
if ('0' <= c && c <= '9' || c == '+' || c == '-')
{
partSb.Append(c);
}
else
{
part = Convert.ToInt64(partSb.ToString());
partSb = new StringBuilder();
sb.Append(FormatInteger(part));
if (c == '.')
sb.Append(".");
else if (c == 'e')
sb.Append("very");
else if (c == 'E')
sb.Append("VERY");
}
}
part = Convert.ToInt64(partSb.ToString());
sb.Append(FormatInteger(part));
return sb.ToString();
case Dtype.STR:
return json.ToString();
default: throw new NotSupportedException();
}
}
}
}
6 changes: 5 additions & 1 deletion JsonToolsNppPlugin/JSONTools/RandomJsonFromSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static JNode RandomString(JNode schema, int minArrayLength, int maxArray
{
for (int ii = 0; ii < length; ii++)
{
char randChar = EXTENDED_ASCII[random.Next(256)];
char randChar = EXTENDED_ASCII[random.Next(1, 256)]; // not allowing \x00 because that will terminate string early in C
sb.Append(randChar);
}
}
Expand Down Expand Up @@ -227,6 +227,10 @@ private static JNode RandomAnything(JNode schema, int minArrayLength, int maxArr
}
}

/// <summary>
/// choose a random schema from an anyOf list of schemas, and make random JSON based on that schema
/// </summary>
/// <returns></returns>
private static JNode RandomAnyOf(JNode anyOf, int minArrayLength, int maxArrayLength, bool extended_ascii_strings)
{
JArray anyOfArr = (JArray)anyOf;
Expand Down
2 changes: 2 additions & 0 deletions JsonToolsNppPlugin/JsonToolsNppPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
<Compile Include="Forms\TreeViewer.Designer.cs">
<DependentUpon>TreeViewer.cs</DependentUpon>
</Compile>
<Compile Include="JSONTools\Dson.cs" />
<Compile Include="PluginInfrastructure\ClikeStringArray.cs" />
<Compile Include="PluginInfrastructure\DllExport\DllExportAttribute.cs" />
<Compile Include="PluginInfrastructure\Docking_h.cs" />
Expand Down Expand Up @@ -193,6 +194,7 @@
<Compile Include="Tests\RandomJsonTests.cs" />
<Compile Include="Tests\RemesPathTests.cs" />
<Compile Include="Tests\SliceTests.cs" />
<Compile Include="Tests\DsonTests.cs" />
<Compile Include="Tests\TestRunner.cs" />
<Compile Include="JSONTools\YamlDumper.cs" />
<Compile Include="Tests\YamlDumperTests.cs" />
Expand Down
25 changes: 22 additions & 3 deletions JsonToolsNppPlugin/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using JSON_Tools.Utils;
using JSON_Tools.Forms;
using JSON_Tools.Tests;
using System.Runtime.InteropServices.WindowsRuntime;

namespace Kbg.NppPluginNET
{
Expand Down Expand Up @@ -49,7 +50,6 @@ class Main
static internal int jsonTreeId = -1;
static internal int grepperFormId = -1;
static internal int AboutFormId = -1;
//static internal int nodeSelectedLabelId = -1;
#endregion

#region " Startup/CleanUp "
Expand Down Expand Up @@ -87,7 +87,8 @@ static internal void CommandMenuInit()
PluginBase.SetCommand(14, "---", null);
PluginBase.SetCommand(15, "JSON to &YAML", DumpYaml);
PluginBase.SetCommand(16, "Run &tests", async () => await TestRunner.RunAll());
PluginBase.SetCommand(17, "A&bout", ShowAboutForm); AboutFormId = 16;
PluginBase.SetCommand(17, "A&bout", ShowAboutForm); AboutFormId = 17;
PluginBase.SetCommand(18, "&Wow such doge", Dogeify);

//// read schemas_to_fname_patterns.json in config directory (if it exists)
//string config_dir = Npp.notepad.GetConfigDirectory();
Expand Down Expand Up @@ -356,7 +357,7 @@ public static JNode TryParseJson(bool is_json_lines = false)
{
int len = Npp.editor.GetLength();
string fname = Npp.notepad.GetCurrentFilePath();
string text = Npp.editor.GetText(len);
string text = Npp.editor.GetText(len + 1);
JNode json = new JNode();
try
{
Expand Down Expand Up @@ -781,6 +782,24 @@ static void ShowAboutForm()
aboutForm.Focus();
}

static void Dogeify()
{
JNode json = TryParseJson();
if (json == null) return;
try
{
string dson = Dson.Dump(json);
Npp.notepad.FileNew();
Npp.editor.SetText(dson);
}
catch (Exception ex)
{
MessageBox.Show($"Could not convert JSON to DSON. Got exception:\r\n{ex}",
"such error very sad",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

//static void MapSchemasToFnamePatterns()
//{
// string config_dir = Npp.notepad.GetConfigDirectory();
Expand Down
14 changes: 14 additions & 0 deletions JsonToolsNppPlugin/PluginInfrastructure/NotepadPPGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public interface INotepadPPGateway
Color GetDefaultForegroundColor();
Color GetDefaultBackgroundColor();
string GetConfigDirectory();
int[] GetNppVersion();
}

/// <summary>
Expand Down Expand Up @@ -174,6 +175,19 @@ public string GetConfigDirectory()
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbIniFilePath);
return sbIniFilePath.ToString();
}

/// <summary>
/// 2-int array. First entry: major version. Second entry: minor version
/// </summary>
/// <returns></returns>
public int[] GetNppVersion()
{
// the low word (i.e., version & 0xffff
int version = Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETNPPVERSION, 0, 0).ToInt32();
int minor = version & 0xffff;
int major = version >> 16;
return new int[] { major, minor };
}
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions JsonToolsNppPlugin/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("4.10.0.2")]
[assembly: AssemblyFileVersion("4.10.0.2")]
[assembly: AssemblyVersion("4.10.0.3")]
[assembly: AssemblyFileVersion("4.10.0.3")]

0 comments on commit e2ffde3

Please sign in to comment.