From 6c2e56d7d99d55b3a4a02f587d2d84c8479fb633 Mon Sep 17 00:00:00 2001 From: lanjian Date: Mon, 20 Feb 2023 16:56:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E8=A7=A3=E6=9E=90=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E6=97=B6=EF=BC=8C=E6=8F=90=E7=A4=BA=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E8=A1=8C=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/jsonhandler.h | 18 ++++++++++++------ src/nddjsonplugin.cpp | 12 ++++++++---- src/scintillaeditor.cpp | 30 ++++++++++++++++++++++++++++++ src/scintillaeditor.h | 17 ++++++++++------- 4 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/jsonhandler.h b/src/jsonhandler.h index 9f41d85..8a914f0 100644 --- a/src/jsonhandler.h +++ b/src/jsonhandler.h @@ -1,6 +1,7 @@ #ifndef JSONHANDLER_H #define JSONHANDLER_H +#include #include #include #include @@ -17,6 +18,8 @@ struct Result { bool success = false; int error_pos = -1; + int errorColumn = -1; + int errorLine = -1; int error_code = -1; std::string error_str; std::string response; @@ -33,7 +36,7 @@ class JsonHandler { ParseOptions m_parseOptions{}; - public: +public: explicit JsonHandler(const ParseOptions &options); ~JsonHandler() = default; @@ -53,6 +56,7 @@ inline auto JsonHandler::ParseJson(const std::string &jsonText, rj::StringBuffer bool success = false; rj::Reader reader; rj::StringStream ss(jsonText.c_str()); + rj::CursorStreamWrapper cursorStreamWrapper(ss); // TODO: Find some better way constexpr auto flgBase_commemt = flgBase | rj::kParseCommentsFlag; @@ -61,22 +65,22 @@ inline auto JsonHandler::ParseJson(const std::string &jsonText, rj::StringBuffer if (m_parseOptions.bIgnoreComment && m_parseOptions.bIgnoreTrailingComma) { - success = reader.Parse(ss, handler) && sb.GetString(); + success = reader.Parse(cursorStreamWrapper, handler) && sb.GetString(); } else if (!m_parseOptions.bIgnoreComment && m_parseOptions.bIgnoreTrailingComma) { - success = reader.Parse(ss, handler) && sb.GetString(); + success = reader.Parse(cursorStreamWrapper, handler) && sb.GetString(); } else if (m_parseOptions.bIgnoreComment && !m_parseOptions.bIgnoreTrailingComma) { - success = reader.Parse(ss, handler) && sb.GetString(); + success = reader.Parse(cursorStreamWrapper, handler) && sb.GetString(); } else if (!m_parseOptions.bIgnoreComment && !m_parseOptions.bIgnoreTrailingComma) { - success = reader.Parse(ss, handler) && sb.GetString(); + success = reader.Parse(cursorStreamWrapper, handler) && sb.GetString(); } if (success) @@ -91,6 +95,8 @@ inline auto JsonHandler::ParseJson(const std::string &jsonText, rj::StringBuffer retVal.success = false; retVal.error_str = rj::GetParseError_En(reader.GetParseErrorCode()); retVal.error_pos = static_cast(reader.GetErrorOffset()); + retVal.errorColumn = static_cast(cursorStreamWrapper.GetColumn()); + retVal.errorLine = static_cast(cursorStreamWrapper.GetLine() - 1); retVal.error_code = reader.GetParseErrorCode(); retVal.response.clear(); } @@ -98,4 +104,4 @@ inline auto JsonHandler::ParseJson(const std::string &jsonText, rj::StringBuffer return retVal; } -#endif // JSONHANDLER_H +#endif // JSONHANDLER_H diff --git a/src/nddjsonplugin.cpp b/src/nddjsonplugin.cpp index 37b44dd..aed5a41 100644 --- a/src/nddjsonplugin.cpp +++ b/src/nddjsonplugin.cpp @@ -146,14 +146,18 @@ void NDDJsonPlugin::reportError(const Result &result) return; } - auto start = scintillaEditor_->getSelectionStart() + result.error_pos; - auto end = start + scintillaEditor_->getSelectionEnd(); + // auto start = scintillaEditor_->getSelectionStart() + result.error_pos; + // auto end = start + scintillaEditor_->getSelectionEnd(); - scintillaEditor_->makeSelection(start, end); + // scintillaEditor_->makeSelection(start, end); // Intimate user std::string err; - err += "\n\nError: (" + std::to_string(result.error_code) + " : " + result.error_str + ")"; + // err += "\n\nError: (" + std::to_string(result.error_code) + " : " + result.error_str + ")"; + err += "\n\nError(line " + std::to_string(result.errorLine) + ", column " + std::to_string(result.errorColumn) + + "): " + result.error_str; + + scintillaEditor_->setCurrentCurPos(result.errorLine, result.errorColumn, result.error_str); showMessage( "JSON Viewer: Error", diff --git a/src/scintillaeditor.cpp b/src/scintillaeditor.cpp index aecd2c9..9398b17 100644 --- a/src/scintillaeditor.cpp +++ b/src/scintillaeditor.cpp @@ -73,6 +73,11 @@ auto ScintillaEditor::getSelectionEnd() const -> size_t auto ScintillaEditor::getEOL() const -> unsigned { auto scintilla_ = scintillaCallback_(); + if (!scintilla_) + { + return {}; + } + auto eolMode = scintilla_->SendScintilla(QsciScintillaBase::SCI_GETEOLMODE); return eolMode; } @@ -80,6 +85,10 @@ auto ScintillaEditor::getEOL() const -> unsigned auto ScintillaEditor::getIndent() const -> std::tuple { auto scintilla_ = scintillaCallback_(); + if (!scintilla_) + { + return {}; + } bool useTabs = scintilla_->SendScintilla(QsciScintillaBase::SCI_GETUSETABS); char indentChar = useTabs ? '\t' : ' '; unsigned indentLen = @@ -91,6 +100,11 @@ auto ScintillaEditor::getIndent() const -> std::tuple void ScintillaEditor::refreshSelectionPos() { auto scintilla_ = scintillaCallback_(); + if (!scintilla_) + { + return; + } + startPos_ = scintilla_->SendScintilla(QsciScintillaBase::SCI_GETSELECTIONSTART); endPos_ = scintilla_->SendScintilla(QsciScintillaBase::SCI_GETSELECTIONEND); @@ -102,7 +116,23 @@ void ScintillaEditor::refreshSelectionPos() void ScintillaEditor::replaceAll(const std::string &text) const { auto scintilla_ = scintillaCallback_(); + if (!scintilla_) + { + return; + } + scintilla_->selectAll(); scintilla_->replaceSelectedText(text.c_str()); scintilla_->setCursorPosition(0, 0); } +void ScintillaEditor::setCurrentCurPos(int line, int column, const std::string &annotateString) +{ + auto scintilla_ = scintillaCallback_(); + if (!scintilla_) + { + return; + } + + scintilla_->setCursorPosition(line, column); + // scintilla_->setMarginText(line, annotateString.c_str(), 0); +} diff --git a/src/scintillaeditor.h b/src/scintillaeditor.h index 00cfa29..468045d 100644 --- a/src/scintillaeditor.h +++ b/src/scintillaeditor.h @@ -5,19 +5,22 @@ #ifndef JSONVIEW_SCINTILLAEDITOR_H #define JSONVIEW_SCINTILLAEDITOR_H -#include #include +#include class QsciScintilla; class ScintillaEditor { - public: - explicit ScintillaEditor(const std::function& cb); +public: + explicit ScintillaEditor(const std::function &cb); ~ScintillaEditor() = default; - public: +public: + void setCurrentCurPos(int line, int column, const std::string &annotateString = {}); + +public: [[nodiscard]] std::string getJsonText(); [[nodiscard]] bool isJsonFile() const; @@ -36,14 +39,14 @@ class ScintillaEditor [[nodiscard]] auto getIndent() const -> std::tuple; - private: +private: void refreshSelectionPos(); - private: +private: std::function scintillaCallback_; size_t startPos_ = 0; size_t endPos_ = 0; }; -#endif // JSONVIEW_SCINTILLAEDITOR_H +#endif // JSONVIEW_SCINTILLAEDITOR_H -- Gitee