Skip to content

Commit 0008b20

Browse files
committed
Re draw tree view on compress/format/sort validate etc.
1 parent bd72554 commit 0008b20

File tree

5 files changed

+47
-18
lines changed

5 files changed

+47
-18
lines changed

src/NppJsonViewer/JsonViewDlg.cpp

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ void JsonViewDlg::FormatJson()
102102

103103
ReportError(res);
104104
}
105+
106+
ReDrawJsonTree();
105107
}
106108

107109
void JsonViewDlg::CompressJson()
@@ -132,6 +134,8 @@ void JsonViewDlg::CompressJson()
132134

133135
ReportError(res);
134136
}
137+
138+
ReDrawJsonTree();
135139
}
136140

137141
void JsonViewDlg::SortJsonByKey()
@@ -164,6 +168,8 @@ void JsonViewDlg::SortJsonByKey()
164168

165169
ReportError(res);
166170
}
171+
172+
ReDrawJsonTree();
167173
}
168174

169175
bool JsonViewDlg::CheckForTokenUndefined(eMethod method, std::string selectedText, Result& res, HTREEITEM tree_root)
@@ -213,7 +219,7 @@ bool JsonViewDlg::CheckForTokenUndefined(eMethod method, std::string selectedTex
213219
else
214220
{
215221
m_pEditor->ReplaceSelection(text);
216-
m_pEditor->MakeSelection(m_pEditor->GetSelectionStart(), static_cast<int>(text.length()));
222+
m_pEditor->MakeSelection(m_pEditor->GetSelectionStart(), text.length());
217223
m_pEditor->RefreshSelectionPos();
218224
}
219225
}
@@ -322,6 +328,8 @@ void JsonViewDlg::ValidateJson()
322328

323329
ReportError(res);
324330
}
331+
332+
DrawJsonTree();
325333
}
326334

327335
void JsonViewDlg::DrawJsonTree()
@@ -375,6 +383,16 @@ void JsonViewDlg::DrawJsonTree()
375383
EnableControls(ctrls, true);
376384
}
377385

386+
void JsonViewDlg::ReDrawJsonTree(bool bForce)
387+
{
388+
const bool bIsVisible = isCreated() && isVisible();
389+
const bool bReDraw = bForce || bIsVisible;
390+
if (bReDraw)
391+
{
392+
DrawJsonTree();
393+
}
394+
}
395+
378396
void JsonViewDlg::HighlightAsJson(bool bForcefully) const
379397
{
380398
bool setJsonLang = bForcefully || m_pSetting->bUseJsonHighlight;
@@ -456,12 +474,12 @@ void JsonViewDlg::UpdateNodePath(HTREEITEM htiNode)
456474

457475
void JsonViewDlg::GoToLine(size_t nLineToGo)
458476
{
459-
m_pEditor->GoToLine(0, nLineToGo);
477+
m_pEditor->GoToLine(nLineToGo);
460478
}
461479

462480
void JsonViewDlg::GoToPosition(size_t nLineToGo, size_t nPos)
463481
{
464-
m_pEditor->GoToPosition(0, nLineToGo, nPos);
482+
m_pEditor->GoToPosition(nLineToGo, nPos);
465483
}
466484

467485
void JsonViewDlg::SearchInTree()
@@ -910,7 +928,7 @@ void JsonViewDlg::HandleTreeEvents(LPARAM lParam)
910928
auto pPosition = GetNodePosition(hItem);
911929
if (pPosition != nullptr)
912930
{
913-
GoToLine(pPosition->nLine - 1); // line index start with 0 in editor, hence --
931+
GoToLine(pPosition->nLine);
914932
}
915933
}
916934
}
@@ -923,7 +941,7 @@ void JsonViewDlg::HandleTreeEvents(LPARAM lParam)
923941
auto pPosition = GetNodePosition(hItem);
924942
if (pPosition != nullptr)
925943
{
926-
GoToPosition(pPosition->nLine - 1, pPosition->nColumn); // line index start with 0 in editor, hence --
944+
GoToPosition(pPosition->nLine, pPosition->nColumn);
927945
}
928946
}
929947
break;

src/NppJsonViewer/JsonViewDlg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class JsonViewDlg : public DockingDlgInterface
5050

5151
private:
5252
void DrawJsonTree();
53+
void ReDrawJsonTree(bool bForce = false);
5354
void HighlightAsJson(bool bForcefully = false) const;
5455
auto PopulateTreeUsingSax(HTREEITEM tree_root, const std::string& jsonText) -> std::optional<std::wstring>;
5556

src/NppJsonViewer/ScintillaEditor.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ auto ScintillaEditor::GetCurrentFileName() const -> std::wstring
6969
void ScintillaEditor::ReplaceSelection(const std::string& text) const
7070
{
7171
::SendMessage(m_hScintilla, SCI_REPLACESEL, 0, reinterpret_cast<LPARAM>(text.c_str()));
72+
73+
// Restore the selection
74+
MakeSelection(m_nStartPos, m_nStartPos + text.length());
7275
}
7376

7477
void ScintillaEditor::MakeSelection(size_t start, size_t end) const
@@ -97,16 +100,18 @@ void ScintillaEditor::RefreshSelectionPos()
97100

98101
if (m_nEndPos < m_nStartPos)
99102
std::swap(m_nStartPos, m_nEndPos);
103+
104+
m_nStartLine = ::SendMessage(m_hScintilla, SCI_LINEFROMPOSITION, m_nStartPos, 0);
100105
}
101106

102-
void ScintillaEditor::GoToLine(size_t nStartLine, size_t nLineToGo) const
107+
void ScintillaEditor::GoToLine(size_t nLineToGo) const
103108
{
104-
::SendMessage(m_hScintilla, SCI_GOTOLINE, nStartLine + nLineToGo, 0);
109+
::SendMessage(m_hScintilla, SCI_GOTOLINE, m_nStartLine + nLineToGo, 0);
105110
}
106111

107-
void ScintillaEditor::GoToPosition(size_t nStartLine, size_t nLineToGo, size_t nColumnIndex) const
112+
void ScintillaEditor::GoToPosition(size_t nLineToGo, size_t nColumnIndex) const
108113
{
109-
size_t lineStartPos = SendMessage(m_hScintilla, SCI_POSITIONFROMLINE, nStartLine + nLineToGo, 0);
114+
size_t lineStartPos = SendMessage(m_hScintilla, SCI_POSITIONFROMLINE, m_nStartLine + nLineToGo, 0);
110115
size_t targetPos = lineStartPos + nColumnIndex;
111116
::SendMessage(m_hScintilla, SCI_GOTOPOS, targetPos, 0);
112117
}

src/NppJsonViewer/ScintillaEditor.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,33 @@ class ScintillaEditor
2929

3030
void ReplaceSelection(const std::string& text) const;
3131

32-
void MakeSelection(size_t start, size_t end) const;
33-
auto GetSelectionStart() const -> size_t
32+
void MakeSelection(size_t start, size_t end) const;
33+
inline auto GetSelectionStart() const -> size_t
3434
{
3535
return m_nStartPos;
3636
}
37-
auto GetSelectionEnd() const -> size_t
37+
inline auto GetSelectionEnd() const -> size_t
3838
{
3939
return m_nEndPos;
4040
}
41+
inline auto GetSelectionStartLine() const -> size_t
42+
{
43+
return m_nStartLine;
44+
}
4145

4246
auto GetEOL() const -> unsigned;
4347
auto GetIndent() const -> std::tuple<char, unsigned>;
4448

4549
void RefreshSelectionPos();
4650

47-
void GoToLine(size_t nStartLine, size_t nLineToGo) const;
48-
void GoToPosition(size_t nStartLine, size_t nLineToGo, size_t nColumnIndex) const;
51+
void GoToLine(size_t nLineToGo) const;
52+
void GoToPosition(size_t nLineToGo, size_t nColumnIndex) const;
4953

5054
private:
5155
NppData m_NppData = {};
5256
HWND m_hScintilla = nullptr;
5357

54-
size_t m_nStartPos = 0;
55-
size_t m_nEndPos = 0;
58+
size_t m_nStartLine = 0;
59+
size_t m_nStartPos = 0;
60+
size_t m_nEndPos = 0;
5661
};

src/NppJsonViewer/TrackingStream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class TrackingStream : public std::enable_shared_from_this<TrackingStream>
1919

2020
TrackingStream(const std::string& jsonText)
2121
: m_ss(jsonText.c_str())
22-
, m_nLine(1)
23-
, m_nColumn(1)
22+
, m_nLine(0)
23+
, m_nColumn(0)
2424
, m_chPrevChar('\0')
2525
{
2626
}

0 commit comments

Comments
 (0)