From 46e810aa53a75c9dcf4edb23dbdf7b96e68349c5 Mon Sep 17 00:00:00 2001 From: Don Ho Date: Sat, 19 Sep 2015 17:18:20 +0200 Subject: [PATCH] Add JSON language support; Add auto-detection for xml, php and bash. --- .../MISC/PluginsManager/Notepad_plus_msgs.h | 2 +- PowerEditor/src/Notepad_plus.cpp | 2 + PowerEditor/src/Notepad_plus.rc | 2 + PowerEditor/src/NppCommands.cpp | 1 + PowerEditor/src/Parameters.cpp | 11 +++- .../ScitillaComponent/ScintillaEditView.cpp | 55 ++++++++++++++----- .../src/ScitillaComponent/ScintillaEditView.h | 1 + PowerEditor/src/langs.model.xml | 3 +- PowerEditor/src/menuCmdID.h | 1 + PowerEditor/src/stylers.model.xml | 7 +++ 10 files changed, 66 insertions(+), 19 deletions(-) diff --git a/PowerEditor/src/MISC/PluginsManager/Notepad_plus_msgs.h b/PowerEditor/src/MISC/PluginsManager/Notepad_plus_msgs.h index de72e8b12..a40453468 100644 --- a/PowerEditor/src/MISC/PluginsManager/Notepad_plus_msgs.h +++ b/PowerEditor/src/MISC/PluginsManager/Notepad_plus_msgs.h @@ -39,7 +39,7 @@ enum LangType {L_TEXT, L_PHP , L_C, L_CPP, L_CS, L_OBJC, L_JAVA, L_RC,\ L_ASM, L_DIFF, L_PROPS, L_PS, L_RUBY, L_SMALLTALK, L_VHDL, L_KIX, L_AU3,\ L_CAML, L_ADA, L_VERILOG, L_MATLAB, L_HASKELL, L_INNO, L_SEARCHRESULT,\ L_CMAKE, L_YAML, L_COBOL, L_GUI4CLI, L_D, L_POWERSHELL, L_R, L_JSP,\ - L_COFFEESCRIPT,\ + L_COFFEESCRIPT, L_JSON,\ // The end of enumated language type, so it should be always at the end L_EXTERNAL}; enum winVer{WV_UNKNOWN, WV_WIN32S, WV_95, WV_98, WV_ME, WV_NT, WV_W2K, WV_XP, WV_S2003, WV_XPX64, WV_VISTA, WV_WIN7, WV_WIN8, WV_WIN81}; diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index 7c13f1b60..e1b65d5be 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -2573,6 +2573,8 @@ enum LangType Notepad_plus::menuID2LangType(int cmdID) return L_XML; case IDM_LANG_JS : return L_JS; + case IDM_LANG_JSON: + return L_JSON; case IDM_LANG_PHP : return L_PHP; case IDM_LANG_ASP : diff --git a/PowerEditor/src/Notepad_plus.rc b/PowerEditor/src/Notepad_plus.rc index 5581a98dd..88518da27 100644 --- a/PowerEditor/src/Notepad_plus.rc +++ b/PowerEditor/src/Notepad_plus.rc @@ -668,6 +668,7 @@ BEGIN MENUITEM "INNO", IDM_LANG_INNO MENUITEM "Java", IDM_LANG_JAVA MENUITEM "Javascript", IDM_LANG_JS + MENUITEM "JSON", IDM_LANG_JSON MENUITEM "JSP", IDM_LANG_JSP MENUITEM "KIXtart", IDM_LANG_KIX MENUITEM "LISP", IDM_LANG_LISP @@ -747,6 +748,7 @@ BEGIN BEGIN MENUITEM "Java", IDM_LANG_JAVA MENUITEM "Javascript", IDM_LANG_JS + MENUITEM "JSON", IDM_LANG_JSON MENUITEM "JSP", IDM_LANG_JSP END MENUITEM "KIXtart", IDM_LANG_KIX diff --git a/PowerEditor/src/NppCommands.cpp b/PowerEditor/src/NppCommands.cpp index 6c43941a7..fee768812 100644 --- a/PowerEditor/src/NppCommands.cpp +++ b/PowerEditor/src/NppCommands.cpp @@ -2377,6 +2377,7 @@ void Notepad_plus::command(int id) case IDM_LANG_HTML : case IDM_LANG_XML : case IDM_LANG_JS : + case IDM_LANG_JSON : case IDM_LANG_PHP : case IDM_LANG_ASP : case IDM_LANG_CSS : diff --git a/PowerEditor/src/Parameters.cpp b/PowerEditor/src/Parameters.cpp index b212cfaf6..929b81e18 100644 --- a/PowerEditor/src/Parameters.cpp +++ b/PowerEditor/src/Parameters.cpp @@ -3444,9 +3444,11 @@ TiXmlNode * NppParameters::getChildElementByAttribut(TiXmlNode *pere, const TCHA LangType NppParameters::getLangIDFromStr(const TCHAR *langName) { int lang = (int)L_TEXT; - for(; lang < L_EXTERNAL; ++lang) { + for(; lang < L_EXTERNAL; ++lang) + { const TCHAR * name = ScintillaEditView::langNames[lang].lexerName; - if (!lstrcmp(name, langName)) { //found lang? + if (!lstrcmp(name, langName)) //found lang? + { return (LangType)lang; } } @@ -3454,7 +3456,8 @@ LangType NppParameters::getLangIDFromStr(const TCHAR *langName) //Cannot find language, check if its an external one LangType l = (LangType)lang; - if (l == L_EXTERNAL) { //try find external lexer + if (l == L_EXTERNAL) //try find external lexer + { int id = _pSelf->getExternalLangIndexFromName(langName); if (id != -1) return (LangType)(id + L_EXTERNAL); } @@ -5887,6 +5890,8 @@ int NppParameters::langTypeToCommandID(LangType lt) const id = IDM_LANG_XML; break; case L_JS : id = IDM_LANG_JS; break; + case L_JSON: + id = IDM_LANG_JSON; break; case L_PHP : id = IDM_LANG_PHP; break; case L_ASP : diff --git a/PowerEditor/src/ScitillaComponent/ScintillaEditView.cpp b/PowerEditor/src/ScitillaComponent/ScintillaEditView.cpp index 9afd51545..ac106762d 100644 --- a/PowerEditor/src/ScitillaComponent/ScintillaEditView.cpp +++ b/PowerEditor/src/ScitillaComponent/ScintillaEditView.cpp @@ -76,14 +76,15 @@ const int ScintillaEditView::_markersArray[][NB_FOLDER_STATE] = { {SC_MARK_BOXMINUS, SC_MARK_BOXPLUS, SC_MARK_VLINE, SC_MARK_LCORNER, SC_MARK_BOXPLUSCONNECTED, SC_MARK_BOXMINUSCONNECTED, SC_MARK_TCORNER} }; -//Array with all the names of all languages +// Array with all the names of all languages +// The order of lang type (enum LangType) must be respected LanguageName ScintillaEditView::langNames[L_EXTERNAL+1] = { {TEXT("normal"), TEXT("Normal text"), TEXT("Normal text file"), L_TEXT, SCLEX_NULL}, {TEXT("php"), TEXT("PHP"), TEXT("PHP Hypertext Preprocessor file"), L_PHP, SCLEX_HTML}, {TEXT("c"), TEXT("C"), TEXT("C source file"), L_C, SCLEX_CPP}, {TEXT("cpp"), TEXT("C++"), TEXT("C++ source file"), L_CPP, SCLEX_CPP}, {TEXT("cs"), TEXT("C#"), TEXT("C# source file"), L_CS, SCLEX_CPP}, -{TEXT("objc"), TEXT("Objective-C"), TEXT("Objective-C source file"), L_OBJC, SCLEX_CPP}, +{TEXT("objc"), TEXT("Objective-C"), TEXT("Objective-C source file"), L_OBJC, SCLEX_CPP}, {TEXT("java"), TEXT("Java"), TEXT("Java source file"), L_JAVA, SCLEX_CPP}, {TEXT("rc"), TEXT("RC"), TEXT("Windows Resource file"), L_RC, SCLEX_CPP}, {TEXT("html"), TEXT("HTML"), TEXT("Hyper Text Markup Language file"), L_HTML, SCLEX_HTML}, @@ -135,6 +136,7 @@ LanguageName ScintillaEditView::langNames[L_EXTERNAL+1] = { {TEXT("r"), TEXT("R"), TEXT("R programming language"), L_R, SCLEX_R}, {TEXT("jsp"), TEXT("JSP"), TEXT("JavaServer Pages script file"), L_JSP, SCLEX_HTML}, {TEXT("coffeescript"), TEXT("CoffeeScript"), TEXT("CoffeeScript file"), L_COFFEESCRIPT, SCLEX_COFFEESCRIPT}, +{ TEXT("json"), TEXT("json"), TEXT("JSON file"), L_JSON, SCLEX_CPP }, {TEXT("ext"), TEXT("External"), TEXT("External"), L_EXTERNAL, SCLEX_NULL} }; @@ -490,28 +492,35 @@ void ScintillaEditView::setStyle(Style styleToSet) { Style & style = stylers.getStyler(i); - if (go.enableFg) { - if (style._colorStyle & COLORSTYLE_FOREGROUND) { + if (go.enableFg) + { + if (style._colorStyle & COLORSTYLE_FOREGROUND) + { styleToSet._colorStyle |= COLORSTYLE_FOREGROUND; styleToSet._fgColor = style._fgColor; - } else { - if (styleToSet._styleID == STYLE_DEFAULT) { //if global is set to transparent, use default style color + } + else + { + if (styleToSet._styleID == STYLE_DEFAULT) //if global is set to transparent, use default style color styleToSet._colorStyle |= COLORSTYLE_FOREGROUND; - } else { + else styleToSet._colorStyle &= ~COLORSTYLE_FOREGROUND; - } } } - if (go.enableBg) { - if (style._colorStyle & COLORSTYLE_BACKGROUND) { + + if (go.enableBg) + { + if (style._colorStyle & COLORSTYLE_BACKGROUND) + { styleToSet._colorStyle |= COLORSTYLE_BACKGROUND; styleToSet._bgColor = style._bgColor; - } else { - if (styleToSet._styleID == STYLE_DEFAULT) { //if global is set to transparent, use default style color + } + else + { + if (styleToSet._styleID == STYLE_DEFAULT) //if global is set to transparent, use default style color styleToSet._colorStyle |= COLORSTYLE_BACKGROUND; - } else { + else styleToSet._colorStyle &= ~COLORSTYLE_BACKGROUND; - } } } if (go.enableFont && style._fontName && style._fontName[0]) @@ -598,6 +607,21 @@ void ScintillaEditView::setEmbeddedJSLexer() execute(SCI_STYLESETEOLFILLED, SCE_HJ_COMMENTDOC, true); } +void ScintillaEditView::setJsonLexer() +{ + execute(SCI_SETLEXER, SCLEX_CPP); + + const TCHAR *pKwArray[10] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; + + makeStyle(L_JSON, pKwArray); + + execute(SCI_SETPROPERTY, reinterpret_cast("fold"), reinterpret_cast("1")); + execute(SCI_SETPROPERTY, reinterpret_cast("fold.compact"), reinterpret_cast("0")); + + execute(SCI_SETPROPERTY, reinterpret_cast("fold.comment"), reinterpret_cast("1")); + execute(SCI_SETPROPERTY, reinterpret_cast("fold.preprocessor"), reinterpret_cast("1")); +} + void ScintillaEditView::setEmbeddedPhpLexer() { const TCHAR *pKwArray[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; @@ -1215,6 +1239,9 @@ void ScintillaEditView::defineDocType(LangType typeDoc) case L_XML : setXmlLexer(typeDoc); break; + case L_JSON: + setJsonLexer(); break; + case L_CSS : setCssLexer(); break; diff --git a/PowerEditor/src/ScitillaComponent/ScintillaEditView.h b/PowerEditor/src/ScitillaComponent/ScintillaEditView.h index 347883e11..972a0eded 100644 --- a/PowerEditor/src/ScitillaComponent/ScintillaEditView.h +++ b/PowerEditor/src/ScitillaComponent/ScintillaEditView.h @@ -699,6 +699,7 @@ protected: void setEmbeddedJSLexer(); void setEmbeddedPhpLexer(); void setEmbeddedAspLexer(); + void setJsonLexer(); //Simple lexers void setCssLexer() { setLexer(SCLEX_CSS, L_CSS, LIST_0 | LIST_1); diff --git a/PowerEditor/src/langs.model.xml b/PowerEditor/src/langs.model.xml index 0997243b8..0ac904cdc 100644 --- a/PowerEditor/src/langs.model.xml +++ b/PowerEditor/src/langs.model.xml @@ -110,9 +110,10 @@ instanceof assert if else switch case default break goto return for while do continue new throw throws try catch finally this super extends implements import true false null package transient strictfp void char short int long double float const static volatile byte boolean class interface native private protected public final abstract synchronized enum - + abstract boolean break byte case catch char class const continue debugger default delete do double else enum export extends final finally float for from function goto if implements import in instanceof int interface let long native new of package private protected public return short static super switch synchronized this throw throws transient try typeof var void volatile while with true false prototype + ? and beep big break call cd cls color cookie1 copy debug del dim display do until exit flushkb for each next function endfunction get gets global go gosub goto if else endif md or password play quit rd redim return run select case endselect set setl setm settime shell sleep small use while loop diff --git a/PowerEditor/src/menuCmdID.h b/PowerEditor/src/menuCmdID.h index 533a0188a..f4bd257f3 100644 --- a/PowerEditor/src/menuCmdID.h +++ b/PowerEditor/src/menuCmdID.h @@ -464,6 +464,7 @@ #define IDM_LANG_R (IDM_LANG + 54) #define IDM_LANG_JSP (IDM_LANG + 55) #define IDM_LANG_COFFEESCRIPT (IDM_LANG + 56) + #define IDM_LANG_JSON (IDM_LANG + 57) #define IDM_LANG_EXTERNAL (IDM_LANG + 65) #define IDM_LANG_EXTERNAL_LIMIT (IDM_LANG + 79) diff --git a/PowerEditor/src/stylers.model.xml b/PowerEditor/src/stylers.model.xml index 3906ffec8..b05e151b1 100644 --- a/PowerEditor/src/stylers.model.xml +++ b/PowerEditor/src/stylers.model.xml @@ -393,6 +393,13 @@ + + + + + + +