Skip to content

Commit 283022e

Browse files
committed
1. Select json node key on double click
2. Fix navigation for json array
1 parent c6bf7c4 commit 283022e

File tree

7 files changed

+24
-12
lines changed

7 files changed

+24
-12
lines changed

src/NppJsonViewer/JsonNode.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ struct Position
1515
{
1616
size_t nLine {};
1717
size_t nColumn {};
18+
size_t nKeyLength {};
1819

1920
void clear()
2021
{
21-
nLine = nColumn = 0;
22+
nLine = nColumn = nKeyLength = 0;
2223
}
2324
};
2425

src/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,9 @@ void JsonViewDlg::GoToLine(size_t nLineToGo)
477477
m_pEditor->GoToLine(nLineToGo);
478478
}
479479

480-
void JsonViewDlg::GoToPosition(size_t nLineToGo, size_t nPos)
480+
void JsonViewDlg::GoToPosition(size_t nLineToGo, size_t nPos, size_t nLen)
481481
{
482-
m_pEditor->GoToPosition(nLineToGo, nPos);
482+
m_pEditor->GoToPosition(nLineToGo, nPos, nLen);
483483
}
484484

485485
void JsonViewDlg::SearchInTree()
@@ -927,7 +927,7 @@ void JsonViewDlg::HandleTreeEvents(LPARAM lParam)
927927
auto pPosition = m_hTreeView->GetNodePosition(hItem);
928928
if (pPosition != nullptr)
929929
{
930-
GoToPosition(pPosition->nLine, pPosition->nColumn);
930+
GoToPosition(pPosition->nLine, pPosition->nColumn, pPosition->nKeyLength);
931931
}
932932
}
933933
break;

src/NppJsonViewer/JsonViewDlg.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class JsonViewDlg : public DockingDlgInterface
5858

5959
void UpdateNodePath(HTREEITEM htiNode);
6060
void GoToLine(size_t nLineToGo);
61-
void GoToPosition(size_t nLineToGo, size_t nPos);
61+
void GoToPosition(size_t nLineToGo, size_t nPos, size_t nLen);
6262

6363
void SearchInTree();
6464

src/NppJsonViewer/RapidJsonHandler.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ bool RapidJsonHandler::String(const Ch* str, unsigned /*length*/, bool /*copy*/)
7979
return true;
8080
}
8181

82-
bool RapidJsonHandler::Key(const Ch* str, unsigned /*length*/, bool /*copy*/)
82+
bool RapidJsonHandler::Key(const Ch* str, unsigned length, bool /*copy*/)
8383
{
84-
m_jsonLastKey.strKey = str;
85-
m_jsonLastKey.pos.nLine = m_pTS->getLine();
86-
m_jsonLastKey.pos.nColumn = m_pTS->getColumn();
84+
m_jsonLastKey.strKey = str;
85+
m_jsonLastKey.pos.nLine = m_pTS->getLine();
86+
m_jsonLastKey.pos.nColumn = m_pTS->getColumn() - length - 1;
87+
m_jsonLastKey.pos.nKeyLength = length;
8788
return true;
8889
}
8990

@@ -198,6 +199,12 @@ void RapidJsonHandler::InsertToTree(TreeNode* node, const char* const str, bool
198199
{
199200
node->node.key.strKey = "[" + std::to_string(node->counter) + "]";
200201
node->node.value = str;
202+
203+
size_t valueLen = node->node.value.size();
204+
node->node.key.pos.nLine = m_pTS->getLine();
205+
node->node.key.pos.nColumn = m_pTS->getColumn() - valueLen - (bQuote ? 1 : 0); // -1 to deal with double quote in string
206+
node->node.key.pos.nKeyLength = valueLen;
207+
201208
node->counter++;
202209
}
203210

src/NppJsonViewer/ScintillaEditor.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,13 @@ void ScintillaEditor::GoToLine(size_t nLineToGo) const
109109
::SendMessage(m_hScintilla, SCI_GOTOLINE, m_nStartLine + nLineToGo, 0);
110110
}
111111

112-
void ScintillaEditor::GoToPosition(size_t nLineToGo, size_t nColumnIndex) const
112+
void ScintillaEditor::GoToPosition(size_t nLineToGo, size_t nColumnIndex, size_t nWordLen, bool selectWord /*= true*/) const
113113
{
114114
size_t lineStartPos = SendMessage(m_hScintilla, SCI_POSITIONFROMLINE, m_nStartLine + nLineToGo, 0);
115115
size_t targetPos = lineStartPos + nColumnIndex;
116116
::SendMessage(m_hScintilla, SCI_GOTOPOS, targetPos, 0);
117+
if (selectWord)
118+
{
119+
MakeSelection(targetPos, targetPos + nWordLen);
120+
}
117121
}

src/NppJsonViewer/ScintillaEditor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class ScintillaEditor
4949
void RefreshSelectionPos();
5050

5151
void GoToLine(size_t nLineToGo) const;
52-
void GoToPosition(size_t nLineToGo, size_t nColumnIndex) const;
52+
void GoToPosition(size_t nLineToGo, size_t nColumnIndex, size_t nWordLen, bool selectWord = true) const;
5353

5454
private:
5555
NppData m_NppData = {};

src/NppJsonViewer/TrackingStream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class TrackingStream : public std::enable_shared_from_this<TrackingStream>
4747
if (ch == '\n')
4848
{
4949
++m_nLine;
50-
m_nColumn = 1;
50+
m_nColumn = 0;
5151
}
5252
else
5353
{

0 commit comments

Comments
 (0)