Skip to content

Commit

Permalink
add doc type button to tree; set() RemesPath func
Browse files Browse the repository at this point in the history
- new document type button above json tree makes it easier
    to change how the document is interpreted (e.g., go
    from JSON mode to REGEX mode or vice versa)
- new set() non-vectorized function in RemesPath
  • Loading branch information
molsonkiko committed Dec 12, 2023
1 parent ff053d8 commit 98d1e82
Show file tree
Hide file tree
Showing 18 changed files with 749 additions and 540 deletions.
18 changes: 10 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- this problem only seems to appear after the user has opened a docking form, and maybe not even every time
- bug with calling arg functions on projections - seems like object projections are treated as arrays when calling arg functions on them in some cases?
- issue with treeview closing when a file with a treeview is moved from one view to another
- `loop()` function used in `s_sub` callbacks is not thread-safe. This doesn't matter right now because RemesPath is single-threaded, but it could matter in the future.
- `loop()` function used in `s_sub` callbacks is not thread-safe. *This doesn't matter right now* because RemesPath is single-threaded, but it could matter in the future.
- __Known issues with `Select This` and `Select all children` commands in tree view (FIX THESE ASAP)__:
* Empty strings are ignored by `Select this`
* Floating point numbers may not be selected correctly (JSON string may have different length from the number representation in the CSV)
Expand All @@ -49,13 +49,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

1. Option to customize which [toolbar icons](/docs/README.md#toolbar-icons) are displayed, and their order.
2. New [regex search form](/docs/README.md#regex-search-form) for using treeview to see regex search results in any file
3. [For loops in RemesPath](/docs/RemesPath.md#for-loopsloop-variables-added-in-v60)
4. [`bool`, `s_csv` and `s_fa` vectorized arg functions](/docs/RemesPath.md#vectorized-functions) and [`randint`, `csv_regex`, and `to_csv` non-vectorized arg functions](/docs/RemesPath.md#non-vectorized-functions) to RemesPath.
5. Make second argument of [`s_split` RemesPath function](/docs/RemesPath.md#vectorized-functions) optional; 1-argument variant splits on whitespace.
6. Right-click dropdown menu in [error form](/docs/README.md#error-form-and-status-bar), allowing export of errors to JSON or refreshing the form.
7. The parser is now much better at recovering when an object is missing its closing `'}'` or an array is missing its closing `']'`.
8. Support for [JSON Schema validation](/docs/README.md#validating-json-against-json-schema) of `enum` keyword where the `type` is missing or an array.
9. `Ctrl+Up` now snaps to parent of currently selected node in tree view. `Ctrl+Down` now snaps to the last direct child of the currently selected node.
3. New [document type list box in tree view](/docs/README.md#document-type-box-added-in-v60)
4. [For loops in RemesPath](/docs/RemesPath.md#for-loopsloop-variables-added-in-v60)
5. [`bool`, `s_csv` and `s_fa` RemesPath vectorized arg functions](/docs/RemesPath.md#vectorized-functions)
6. [`randint`, `csv_regex`, `set`, and `to_csv` RemesPath non-vectorized arg functions](/docs/RemesPath.md#non-vectorized-functions)
7. Make second argument of [`s_split` RemesPath function](/docs/RemesPath.md#vectorized-functions) optional; 1-argument variant splits on whitespace.
8. Right-click dropdown menu in [error form](/docs/README.md#error-form-and-status-bar), allowing export of errors to JSON or refreshing the form.
9. The parser is now much better at recovering when an object is missing its closing `'}'` or an array is missing its closing `']'`.
10. Support for [JSON Schema validation](/docs/README.md#validating-json-against-json-schema) of `enum` keyword where the `type` is missing or an array.
11. `Ctrl+Up` now snaps to parent of currently selected node in tree view. `Ctrl+Down` now snaps to the last direct child of the currently selected node.

### Changed

Expand Down
62 changes: 41 additions & 21 deletions JsonToolsNppPlugin/Forms/TreeViewer.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 53 additions & 3 deletions JsonToolsNppPlugin/Forms/TreeViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public partial class TreeViewer : Form
/// </summary>
private bool isExpandingAllSubtrees;

/// <summary>
/// avoid unnecessary parsing if a change in the selected index of the DocumentTypeComboBox was not performed manually (in which case this is true)
/// </summary>
public bool documentTypeIndexChangeWasAutomatic;

/// <summary>
/// If the user performs an undo or redo action,
/// this will be set to true so that the next time the user performs a RemesPath query,
Expand Down Expand Up @@ -90,6 +95,9 @@ public TreeViewer(JNode json)
findReplaceForm = null;
csvDelim = '\x00';
csvQuote = '\x00';
documentTypeIndexChangeWasAutomatic = true; // avoid parsing twice on initialization
SetDocumentTypeListBoxIndex(GetDocumentType());
documentTypeIndexChangeWasAutomatic = false;
FormStyle.ApplyStyle(this, Main.settings.use_npp_styling);
}

Expand Down Expand Up @@ -149,7 +157,8 @@ private void TreeViewer_KeyUp(object sender, KeyEventArgs e)
else if (e.KeyCode == Keys.Tab)
{
Control next = GetNextControl((Control)sender, !e.Shift);
while ((next == null) || (!next.TabStop)) next = GetNextControl(next, !e.Shift);
while (next is null || !next.TabStop)
next = GetNextControl(next, !e.Shift);
next.Focus();
}
else if (sender is TreeView && e.Control)
Expand All @@ -158,6 +167,8 @@ private void TreeViewer_KeyUp(object sender, KeyEventArgs e)
if (e.KeyCode == Keys.Up)
{
TreeNode selected = Tree.SelectedNode;
if (selected is null)
return;
TreeNode parent = selected.Parent;
if (parent is null)
return;
Expand All @@ -167,7 +178,7 @@ private void TreeViewer_KeyUp(object sender, KeyEventArgs e)
else if (e.KeyCode == Keys.Down)
{
TreeNode selected = Tree.SelectedNode;
if (selected.Nodes.Count > 0)
if (!(selected is null) && selected.Nodes.Count > 0)
{
if (!selected.IsExpanded)
selected.Expand();
Expand Down Expand Up @@ -1149,7 +1160,7 @@ private void RefreshButton_Click(object sender, EventArgs e)
{
shouldRefresh = false;
string cur_fname = Npp.notepad.GetCurrentFilePath();
(bool _, JNode new_json, bool _, DocumentType _) = Main.TryParseJson();
(bool _, JNode new_json, bool _, DocumentType _) = Main.TryParseJson(GetDocumentTypeFromListBox());
if (new_json == null)
return;
fname = cur_fname;
Expand All @@ -1173,6 +1184,45 @@ private void TreeViewer_DoubleClick(object sender, EventArgs e)
Npp.notepad.OpenFile(fname);
}

public void SetDocumentTypeListBoxIndex(DocumentType documentType)
{
switch (documentType)
{
case DocumentType.NONE:
case DocumentType.JSON:
DocumentTypeListBox.SelectedIndex = 0;
break;
case DocumentType.JSONL: DocumentTypeListBox.SelectedIndex = 1; break;
case DocumentType.INI: DocumentTypeListBox.SelectedIndex = 2; break;
case DocumentType.REGEX: DocumentTypeListBox.SelectedIndex = 3; break;
default: break;
}
}

public DocumentType GetDocumentTypeFromListBox()
{
switch (DocumentTypeListBox.SelectedIndex)
{
case 0: return DocumentType.JSON;
case 1: return DocumentType.JSONL;
case 2: return DocumentType.INI;
case 3: return DocumentType.REGEX;
default: return DocumentType.NONE;
}
}

private void DocumentTypeListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (documentTypeIndexChangeWasAutomatic)
return;
DocumentType newDocumentType = GetDocumentTypeFromListBox();
DocumentType oldDocumentType = GetDocumentType();
if (oldDocumentType == newDocumentType)
return;
(_, JNode newJson, _, _) =Main.TryParseJson(newDocumentType);
JsonTreePopulate(newJson);
}

/// <summary>
/// Just the filename, no directory information.<br></br>
/// If no fname supplied, gets the relative filename for this TreeViewer's fname.
Expand Down
2 changes: 1 addition & 1 deletion JsonToolsNppPlugin/Forms/TreeViewer.resx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAADC
CgAAAk1TRnQBSQFMAgEBCAEAAZgBAQGYAQEBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
CgAAAk1TRnQBSQFMAgEBCAEAAbABAQGwAQEBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
AwABQAMAATADAAEBAQABCAYAAQwYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA
AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5
AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA
Expand Down
2 changes: 1 addition & 1 deletion JsonToolsNppPlugin/JSONTools/JNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,7 @@ public JNode GetQuery()
/// <returns></returns>
public JNode Evaluate(JNode inp)
{
ArgFunction.InitializeGlobals(mutatesInput);
JNode lastStatement = EvaluateStatementsFromStartToEnd(inp, 0, statements.Count);
Reset();
return lastStatement;
Expand Down Expand Up @@ -2062,7 +2063,6 @@ private JNode EvaluateStatementsFromStartToEnd(JNode inp, int start, int end)
{
JNode lastStatement = null;
indexInStatements = start;
ArgFunction.regexSearchResultsShouldBeCached = !mutatesInput;
while (indexInStatements < end)
{
JNode statement = statements[indexInStatements];
Expand Down
4 changes: 1 addition & 3 deletions JsonToolsNppPlugin/JSONTools/RemesPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,8 +1198,6 @@ private JNode ParseQuery(List<object> toks)
int pos = 0;
int end = toks.Count;
var context = new JQueryContext();
ArgFunction.csvDelimiterInLastQuery = '\x00';
ArgFunction.csvQuoteCharInLastQuery = '\x00';
Dictionary<string, bool> isVarnameFunctionOfInput = DetermineWhichVariablesAreFunctionsOfInput(toks);
while (pos < end)
{
Expand Down Expand Up @@ -1243,7 +1241,7 @@ private Dictionary<string, bool> DetermineWhichVariablesAreFunctionsOfInput(List
else
pos = endOfStatement + 1;
}
ArgFunction.regexSearchResultsShouldBeCached = !containsMutation; // any mutation could potentially mutate values inside the cache, which is very bad
ArgFunction.InitializeGlobals(containsMutation);
if (containsMutation)
{
// no variable is safe from the mutation, not even ones that were declared before the mutation expression
Expand Down
Loading

0 comments on commit 98d1e82

Please sign in to comment.