From 3b9d8c651baa5f4647ef25950c1c628a7c0a340a Mon Sep 17 00:00:00 2001 From: leoshone Date: Sat, 5 Sep 2026 12:51:42 +0800 Subject: [PATCH] Recognize .jsonc without implying JSON5 support IsJsonFile() used to accept every L_JSON5 document. Notepad++ maps the .jsonc extension to the json5 language ("json5 jsonc" share one entry in the default langs.xml), but that also pulled in genuine .json5 documents, whose syntax (unquoted keys, single-quoted strings) the parser does not support: they used to be silently ignored by the plugin and would now be parsed and reported as an error. Accept the json5 language for .jsonc files only, so a real .json5 file keeps behaving exactly as before this change. Verified end to end: .jsonc and .json5 are both reported as the same language (86) by Notepad++, the .jsonc document is drawn while the .json5 one is not, and the existing jsonc assertions still pass. --- src/NppJsonViewer/ScintillaEditor.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/NppJsonViewer/ScintillaEditor.cpp b/src/NppJsonViewer/ScintillaEditor.cpp index 613b6f5..db96a08 100644 --- a/src/NppJsonViewer/ScintillaEditor.cpp +++ b/src/NppJsonViewer/ScintillaEditor.cpp @@ -1,5 +1,5 @@ #include "ScintillaEditor.h" - +#include "StringHelper.h" #include #include @@ -57,7 +57,25 @@ bool ScintillaEditor::IsJsonFile() const { unsigned languageType = 0; ::SendMessage(m_NppData._nppHandle, NPPM_GETCURRENTLANGTYPE, 0, reinterpret_cast(&languageType)); - return languageType == LangType::L_JSON; + + if (languageType == LangType::L_JSON) + return true; + + // Notepad++ maps the .jsonc extension to the json5 language ("json5 jsonc" + // share a single entry in the default langs.xml), so a jsonc document used + // to be skipped by everything gated on IsJsonFile(). Accept that language + // for .jsonc files ONLY: the parser handles comments and trailing commas + // (both are configurable and on by default) but not the rest of the JSON5 + // syntax (unquoted keys, single-quoted strings), and a real .json5 file + // must keep being ignored exactly as it was before this change. + if (languageType == LangType::L_JSON5) + { + auto fileName = GetCurrentFileName(); + StringHelper::ToLower(fileName); + return fileName.ends_with(L".jsonc"); + } + + return false; } auto ScintillaEditor::GetCurrentFileName() const -> std::wstring