Skip to content

Commit

Permalink
1. Select json node key on double click
Browse files Browse the repository at this point in the history
2. Fix navigation for json array
  • Loading branch information
SinghRajenM committed Oct 26, 2024
1 parent c6bf7c4 commit 283022e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/NppJsonViewer/JsonNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ struct Position
{
size_t nLine {};
size_t nColumn {};
size_t nKeyLength {};

void clear()
{
nLine = nColumn = 0;
nLine = nColumn = nKeyLength = 0;
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/NppJsonViewer/JsonViewDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,9 @@ void JsonViewDlg::GoToLine(size_t nLineToGo)
m_pEditor->GoToLine(nLineToGo);
}

void JsonViewDlg::GoToPosition(size_t nLineToGo, size_t nPos)
void JsonViewDlg::GoToPosition(size_t nLineToGo, size_t nPos, size_t nLen)
{
m_pEditor->GoToPosition(nLineToGo, nPos);
m_pEditor->GoToPosition(nLineToGo, nPos, nLen);
}

void JsonViewDlg::SearchInTree()
Expand Down Expand Up @@ -927,7 +927,7 @@ void JsonViewDlg::HandleTreeEvents(LPARAM lParam)
auto pPosition = m_hTreeView->GetNodePosition(hItem);
if (pPosition != nullptr)
{
GoToPosition(pPosition->nLine, pPosition->nColumn);
GoToPosition(pPosition->nLine, pPosition->nColumn, pPosition->nKeyLength);
}
}
break;
Expand Down
2 changes: 1 addition & 1 deletion src/NppJsonViewer/JsonViewDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class JsonViewDlg : public DockingDlgInterface

void UpdateNodePath(HTREEITEM htiNode);
void GoToLine(size_t nLineToGo);
void GoToPosition(size_t nLineToGo, size_t nPos);
void GoToPosition(size_t nLineToGo, size_t nPos, size_t nLen);

void SearchInTree();

Expand Down
15 changes: 11 additions & 4 deletions src/NppJsonViewer/RapidJsonHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ bool RapidJsonHandler::String(const Ch* str, unsigned /*length*/, bool /*copy*/)
return true;
}

bool RapidJsonHandler::Key(const Ch* str, unsigned /*length*/, bool /*copy*/)
bool RapidJsonHandler::Key(const Ch* str, unsigned length, bool /*copy*/)
{
m_jsonLastKey.strKey = str;
m_jsonLastKey.pos.nLine = m_pTS->getLine();
m_jsonLastKey.pos.nColumn = m_pTS->getColumn();
m_jsonLastKey.strKey = str;
m_jsonLastKey.pos.nLine = m_pTS->getLine();
m_jsonLastKey.pos.nColumn = m_pTS->getColumn() - length - 1;
m_jsonLastKey.pos.nKeyLength = length;
return true;
}

Expand Down Expand Up @@ -198,6 +199,12 @@ void RapidJsonHandler::InsertToTree(TreeNode* node, const char* const str, bool
{
node->node.key.strKey = "[" + std::to_string(node->counter) + "]";
node->node.value = str;

size_t valueLen = node->node.value.size();
node->node.key.pos.nLine = m_pTS->getLine();
node->node.key.pos.nColumn = m_pTS->getColumn() - valueLen - (bQuote ? 1 : 0); // -1 to deal with double quote in string
node->node.key.pos.nKeyLength = valueLen;

node->counter++;
}

Expand Down
6 changes: 5 additions & 1 deletion src/NppJsonViewer/ScintillaEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,13 @@ void ScintillaEditor::GoToLine(size_t nLineToGo) const
::SendMessage(m_hScintilla, SCI_GOTOLINE, m_nStartLine + nLineToGo, 0);
}

void ScintillaEditor::GoToPosition(size_t nLineToGo, size_t nColumnIndex) const
void ScintillaEditor::GoToPosition(size_t nLineToGo, size_t nColumnIndex, size_t nWordLen, bool selectWord /*= true*/) const
{
size_t lineStartPos = SendMessage(m_hScintilla, SCI_POSITIONFROMLINE, m_nStartLine + nLineToGo, 0);
size_t targetPos = lineStartPos + nColumnIndex;
::SendMessage(m_hScintilla, SCI_GOTOPOS, targetPos, 0);
if (selectWord)
{
MakeSelection(targetPos, targetPos + nWordLen);
}
}
2 changes: 1 addition & 1 deletion src/NppJsonViewer/ScintillaEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ScintillaEditor
void RefreshSelectionPos();

void GoToLine(size_t nLineToGo) const;
void GoToPosition(size_t nLineToGo, size_t nColumnIndex) const;
void GoToPosition(size_t nLineToGo, size_t nColumnIndex, size_t nWordLen, bool selectWord = true) const;

private:
NppData m_NppData = {};
Expand Down
2 changes: 1 addition & 1 deletion src/NppJsonViewer/TrackingStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TrackingStream : public std::enable_shared_from_this<TrackingStream>
if (ch == '\n')
{
++m_nLine;
m_nColumn = 1;
m_nColumn = 0;
}
else
{
Expand Down

0 comments on commit 283022e

Please sign in to comment.