Skip to content

Commit

Permalink
Add HTML highlighting in PHP files (#691)
Browse files Browse the repository at this point in the history
Closes #409
  • Loading branch information
dail8859 authored Dec 27, 2024
1 parent 34fecf2 commit 91471cd
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 45 deletions.
41 changes: 2 additions & 39 deletions src/NotepadNext/NotepadNextApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,44 +321,7 @@ void NotepadNextApplication::setEditorLanguage(ScintillaNext *editor, const QStr
getLuaState()->execute(QString("skip_tabs = %1").arg(editor->QObject::property("nn_skip_usetabs").isValid() ? "true" : "false").toLatin1().constData());
getLuaState()->execute(QString("skip_tabwidth = %1").arg(editor->QObject::property("nn_skip_tabwidth").isValid() ? "true" : "false").toLatin1().constData());

getLuaState()->execute(R"(
local L = languages[languageName]
if not skip_tabs then
editor.UseTabs = (L.tabSettings or "tabs") == "tabs"
end
if not skip_tabwidth then
editor.TabWidth = L.tabSize or 4
end
editor.MarginWidthN[2] = L.disableFoldMargin and 0 or 16
if L.styles then
for name, style in pairs(L.styles) do
editor.StyleFore[style.id] = style.fgColor
editor.StyleBack[style.id] = style.bgColor
if style.fontStyle then
editor.StyleBold[style.id] = (style.fontStyle & 1 == 1)
editor.StyleItalic[style.id] = (style.fontStyle & 2 == 2)
editor.StyleUnderline[style.id] = (style.fontStyle & 4 == 4)
editor.StyleEOLFilled[style.id] = (style.fontStyle & 8 == 8)
end
end
end
if L.keywords then
for id, kw in pairs(L.keywords) do
editor.KeyWords[id] = kw
end
end
if L.properties then
for p,v in pairs(L.properties) do
editor.Property[p] = v
end
end
editor.Property["fold"] = "1"
editor.Property["fold.compact"] = "0"
)");
getLuaState()->execute(QString("SetLanguage(languageName)").toLatin1().constData());
}

QString NotepadNextApplication::detectLanguage(ScintillaNext *editor) const
Expand Down Expand Up @@ -407,7 +370,7 @@ QString NotepadNextApplication::detectLanguageFromContents(ScintillaNext *editor
-- Grab a small chunk
if editor.Length > 0 then
editor:SetTargetRange(0, math.min(64, editor.Length))
return detectLanguageFromContents(editor.TargetText)
return DetectLanguageFromContents(editor.TargetText)
end
return "Text"
Expand Down
7 changes: 6 additions & 1 deletion src/NotepadNext/languages/php.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local L = {}

L.lexer = "phpscript"
L.lexer = "hypertext"

L.singleLineComment = "// "

Expand Down Expand Up @@ -87,4 +87,9 @@ L.styles = {
bgColor = rgb(0xFEFCF5),
},
}

L.additionalLanguages = {
"HTML"
}

return L
60 changes: 55 additions & 5 deletions src/NotepadNext/scripts/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ function rgb(x)
return ((x & 0xFF) << 16) | (x & 0xFF00) | ((x & 0xFF0000) >> 16)
end

function starts_with(str, start)
return str:sub(1, #start) == start
end

function detectLanguageFromContents(contents)
function DetectLanguageFromContents(contents)
for name, L in pairs(languages) do
if L.first_line then
for _, pattern in ipairs(L.first_line) do
Expand Down Expand Up @@ -52,6 +48,60 @@ function DialogFilters()
return table.concat(filters, ";;")
end

function SetStyle(L)
if L.styles then
for _, style in pairs(L.styles) do
editor.StyleFore[style.id] = style.fgColor
editor.StyleBack[style.id] = style.bgColor

if style.fontStyle then
editor.StyleBold[style.id] = (style.fontStyle & 1 == 1)
editor.StyleItalic[style.id] = (style.fontStyle & 2 == 2)
editor.StyleUnderline[style.id] = (style.fontStyle & 4 == 4)
editor.StyleEOLFilled[style.id] = (style.fontStyle & 8 == 8)
end
end
end

if L.keywords then
for id, kw in pairs(L.keywords) do
editor.KeyWords[id] = kw
end
end

if L.properties then
for p, v in pairs(L.properties) do
editor.Property[p] = v
end
end
end

function SetLanguage(languageName)
local L = languages[languageName]

if not skip_tabs then
editor.UseTabs = (L.tabSettings or "tabs") == "tabs"
end

if not skip_tabwidth then
editor.TabWidth = L.tabSize or 4
end

editor.MarginWidthN[2] = L.disableFoldMargin and 0 or 16

SetStyle(L)

if L.additionalLanguages then
for _, language in pairs(L.additionalLanguages) do
SetStyle(languages[language])
end
end


editor.Property["fold"] = "1"
editor.Property["fold.compact"] = "0"
end

languages = {}
languages["ActionScript"] = require("actionscript")
languages["ADA"] = require("ada")
Expand Down

0 comments on commit 91471cd

Please sign in to comment.