Skip to content

Commit

Permalink
Excplicit callout for no support on multiline selection
Browse files Browse the repository at this point in the history
#fixes #197 #closes #197,
  • Loading branch information
SinghRajenM committed Sep 23, 2024
1 parent 98dae7d commit 3d85bd1
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 24 deletions.
9 changes: 5 additions & 4 deletions src/NppJsonViewer/Define.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ const TCHAR JSON_ERROR_TITLE[] = TEXT("JSON Viewer: Error");
const TCHAR JSON_WARNING_TITLE[] = TEXT("JSON Viewer: Warning");
const TCHAR JSON_INFO_TITLE[] = TEXT("JSON Viewer: Information");

const TCHAR JSON_ERR_PARSE[] = TEXT("Cannot parse JSON. Please select a JSON string.");
const TCHAR JSON_ERR_VALIDATE[] = TEXT("There was an error while parsing JSON. Refer to the current selection for possible problematic area.");
const TCHAR JSON_ERR_VALIDATE_SUCCESS[] = TEXT("JSON looks good. No errors found while validating it.");
const TCHAR JSON_ERR_SAVE_SETTING[] = TEXT("Failed to save the settings. Please try again.");
const TCHAR JSON_ERR_PARSE[] = TEXT("Unable to parse JSON. Please ensure a valid JSON string is selected.");
const TCHAR JSON_ERR_VALIDATE[] = TEXT("An error occurred while parsing the JSON. Check the current selection for the potential issue.");
const TCHAR JSON_ERR_VALIDATE_SUCCESS[] = TEXT("The JSON appears valid. No errors were found during validation.");
const TCHAR JSON_ERR_SAVE_SETTING[] = TEXT("Could not save the settings. Please try again.");
const TCHAR JSON_ERR_MULTI_SELECTION[] = TEXT("Multiline selection is not currently supported in Json Viewer.");

const TCHAR STR_VERSION[] = TEXT("Version: ");
const TCHAR STR_COPY[] = TEXT("Copy");
Expand Down
118 changes: 101 additions & 17 deletions src/NppJsonViewer/JsonViewDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,19 @@ void JsonViewDlg::ShowDlg(bool bShow)

void JsonViewDlg::FormatJson()
{
const auto selectedText = m_pEditor->GetJsonText();
const auto selectedData = m_pEditor->GetJsonText();
const auto selectedText = IsSelectionValidJson(selectedData);

if (!selectedText.has_value() || selectedText.value().empty())
{
const std::wstring msg = IsMultiSelection(selectedData) ? JSON_ERR_MULTI_SELECTION : JSON_ERR_PARSE;
ShowMessage(JSON_INFO_TITLE, msg, MB_OK | MB_ICONINFORMATION);
return;
}

auto [le, lf, indentChar, indentLen] = GetFormatSetting();

Result res = JsonHandler(m_pSetting->parseOptions).FormatJson(selectedText, le, lf, indentChar, indentLen);
Result res = JsonHandler(m_pSetting->parseOptions).FormatJson(selectedText.value(), le, lf, indentChar, indentLen);

if (res.success)
{
Expand All @@ -85,7 +94,7 @@ void JsonViewDlg::FormatJson()
}
else
{
if (CheckForTokenUndefined(JsonViewDlg::eMethod::FormatJson, selectedText, res, NULL))
if (CheckForTokenUndefined(JsonViewDlg::eMethod::FormatJson, selectedText.value(), res, NULL))
return;

ReportError(res);
Expand All @@ -94,10 +103,17 @@ void JsonViewDlg::FormatJson()

void JsonViewDlg::CompressJson()
{
// Get the current scintilla
const auto selectedText = m_pEditor->GetJsonText();
const auto selectedData = m_pEditor->GetJsonText();
const auto selectedText = IsSelectionValidJson(selectedData);

Result res = JsonHandler(m_pSetting->parseOptions).GetCompressedJson(selectedText);
if (!selectedText.has_value() || selectedText.value().empty())
{
const std::wstring msg = IsMultiSelection(selectedData) ? JSON_ERR_MULTI_SELECTION : JSON_ERR_PARSE;
ShowMessage(JSON_INFO_TITLE, msg, MB_OK | MB_ICONINFORMATION);
return;
}

Result res = JsonHandler(m_pSetting->parseOptions).GetCompressedJson(selectedText.value());

if (res.success)
{
Expand All @@ -106,7 +122,7 @@ void JsonViewDlg::CompressJson()
}
else
{
if (CheckForTokenUndefined(JsonViewDlg::eMethod::GetCompressedJson, selectedText, res, NULL))
if (CheckForTokenUndefined(JsonViewDlg::eMethod::GetCompressedJson, selectedText.value(), res, NULL))
return;

ReportError(res);
Expand All @@ -115,10 +131,19 @@ void JsonViewDlg::CompressJson()

void JsonViewDlg::SortJsonByKey()
{
const auto selectedText = m_pEditor->GetJsonText();
const auto selectedData = m_pEditor->GetJsonText();
const auto selectedText = IsSelectionValidJson(selectedData);

if (!selectedText.has_value() || selectedText.value().empty())
{
const std::wstring msg = IsMultiSelection(selectedData) ? JSON_ERR_MULTI_SELECTION : JSON_ERR_PARSE;
ShowMessage(JSON_INFO_TITLE, msg, MB_OK | MB_ICONINFORMATION);
return;
}

auto [le, lf, indentChar, indentLen] = GetFormatSetting();

Result res = JsonHandler(m_pSetting->parseOptions).SortJsonByKey(selectedText, le, lf, indentChar, indentLen);
Result res = JsonHandler(m_pSetting->parseOptions).SortJsonByKey(selectedText.value(), le, lf, indentChar, indentLen);

if (res.success)
{
Expand All @@ -127,7 +152,7 @@ void JsonViewDlg::SortJsonByKey()
}
else
{
if (CheckForTokenUndefined(JsonViewDlg::eMethod::SortJsonByKey, selectedText, res, NULL))
if (CheckForTokenUndefined(JsonViewDlg::eMethod::SortJsonByKey, selectedText.value(), res, NULL))
return;

ReportError(res);
Expand Down Expand Up @@ -199,6 +224,52 @@ bool JsonViewDlg::CheckForTokenUndefined(eMethod method, std::string selectedTex
return false;
}

bool JsonViewDlg::IsMultiSelection(const ScintillaData &scintillaData) const
{
std::string text;
ScintillaCode code = ScintillaCode::Unknown;

ProcessScintillaData(scintillaData, text, code);

bool bRetVal = code == ScintillaCode::MultiLineSelection ? true : false;
return bRetVal;
}

auto JsonViewDlg::IsSelectionValidJson(const ScintillaData &scintillaData) const -> std::optional<std::string>
{
std::string text;
ScintillaCode code = ScintillaCode::Unknown;

ProcessScintillaData(scintillaData, text, code);

if (code == ScintillaCode::Success)
return text;

return std::nullopt;
}

void JsonViewDlg::ProcessScintillaData(const ScintillaData &scintillaData, std::string &text, ScintillaCode &code) const
{
text.clear();
code = ScintillaCode::Unknown;

std::visit(
[&text, &code](auto &&arg)
{
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, std::string>)
{
text = arg;
code = ScintillaCode::Success;
}
else if constexpr (std::is_same_v<T, ScintillaCode>)
{
code = arg;
}
},
scintillaData);
}

void JsonViewDlg::HandleTabActivated()
{
const bool bIsVisible = isCreated() && isVisible();
Expand All @@ -223,18 +294,25 @@ void JsonViewDlg::HandleTabActivated()

void JsonViewDlg::ValidateJson()
{
// Get the current scintilla
const auto selectedText = m_pEditor->GetJsonText();
const auto selectedData = m_pEditor->GetJsonText();
const auto selectedText = IsSelectionValidJson(selectedData);

if (!selectedText.has_value() || selectedText.value().empty())
{
const std::wstring msg = IsMultiSelection(selectedData) ? JSON_ERR_MULTI_SELECTION : JSON_ERR_PARSE;
ShowMessage(JSON_INFO_TITLE, msg, MB_OK | MB_ICONINFORMATION);
return;
}

Result res = JsonHandler(m_pSetting->parseOptions).ValidateJson(selectedText);
Result res = JsonHandler(m_pSetting->parseOptions).ValidateJson(selectedText.value());

if (res.success)
{
ShowMessage(JSON_INFO_TITLE, JSON_ERR_VALIDATE_SUCCESS, MB_OK | MB_ICONINFORMATION);
}
else
{
if (CheckForTokenUndefined(JsonViewDlg::eMethod::ValidateJson, selectedText, res, NULL))
if (CheckForTokenUndefined(JsonViewDlg::eMethod::ValidateJson, selectedText.value(), res, NULL))
{
ShowMessage(JSON_INFO_TITLE, JSON_ERR_VALIDATE_SUCCESS, MB_OK | MB_ICONINFORMATION);
return;
Expand All @@ -255,15 +333,21 @@ void JsonViewDlg::DrawJsonTree()

// Refresh the view
m_pEditor->RefreshViewHandle();
const std::string txtForParsing = m_pEditor->GetJsonText();
const auto selectedData = m_pEditor->GetJsonText();
const auto selectedText = IsSelectionValidJson(selectedData);

if (txtForParsing.empty())
if (!selectedText.has_value() || selectedText.value().empty())
{
m_hTreeView->InsertNode(JSON_ERR_PARSE, NULL, rootNode);

if (IsMultiSelection(selectedData))
{
ShowMessage(JSON_INFO_TITLE, JSON_ERR_MULTI_SELECTION, MB_OK | MB_ICONINFORMATION);
}
}
else
{
auto res = PopulateTreeUsingSax(rootNode, txtForParsing);
auto res = PopulateTreeUsingSax(rootNode, selectedText.value());
if (res.has_value())
{
// This is the case when Notepad++ has JsonViewer Window opened for previous instance
Expand Down
4 changes: 4 additions & 0 deletions src/NppJsonViewer/JsonViewDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class JsonViewDlg : public DockingDlgInterface

bool CheckForTokenUndefined(eMethod method, std::string selectedText, Result &res, HTREEITEM tree_root);

bool IsMultiSelection(const ScintillaData &scintillaData) const;
auto IsSelectionValidJson(const ScintillaData &scintillaData) const -> std::optional<std::string>;
void ProcessScintillaData(const ScintillaData &scintillaData, std::string &text, ScintillaCode &code) const;

protected:
virtual INT_PTR CALLBACK run_dlgProc(UINT message, WPARAM wParam, LPARAM lParam) override;

Expand Down
9 changes: 7 additions & 2 deletions src/NppJsonViewer/ScintillaEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ void ScintillaEditor::RefreshViewHandle()
m_hScintilla = (which == 0) ? m_NppData._scintillaMainHandle : m_NppData._scintillaSecondHandle;
}

auto ScintillaEditor::GetJsonText() -> std::string
auto ScintillaEditor::GetJsonText() -> ScintillaData
{
if (!m_hScintilla)
return std::string();
return ScintillaCode::NotInitialized;

// Multi selection is not supported
size_t nSelections = ::SendMessage(m_hScintilla, SCI_GETSELECTIONS, 0, 0);
if (nSelections > 1)
return ScintillaCode::MultiLineSelection;

// Adjust the selection position
RefreshSelectionPos();
Expand Down
14 changes: 13 additions & 1 deletion src/NppJsonViewer/ScintillaEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@
#include "Define.h"
#include <string>
#include <tuple>
#include <variant>

enum class ScintillaCode : short
{
Unknown,
Success,
NotInitialized,
NoSelection,
MultiLineSelection
};

using ScintillaData = std::variant<std::string, ScintillaCode>;

class ScintillaEditor
{
Expand All @@ -10,7 +22,7 @@ class ScintillaEditor
~ScintillaEditor() = default;

void RefreshViewHandle();
auto GetJsonText() -> std::string;
auto GetJsonText() -> ScintillaData;
void SetLangAsJson() const;
bool IsJsonFile() const;
auto GetCurrentFileName() const -> std::wstring;
Expand Down

0 comments on commit 3d85bd1

Please sign in to comment.