Fix background highlighting of included highlighters.

[GB.HIGHLIGHT]
* BUG: Fix background highlighting of included highlighters.
This commit is contained in:
Benoît Minisini 2023-11-03 20:33:42 +01:00
parent 173e9a699d
commit a1db0cf233
7 changed files with 69 additions and 50 deletions

View file

@ -38,16 +38,14 @@ Public Sub Compile(hState As CState)
CState.Print("sWord = Match(" & Quote($sFrom) & ")") CState.Print("sWord = Match(" & Quote($sFrom) & ")")
CState.Print("If Not sWord Then Goto " & hState.GetNextLabel()) CState.Print("If Not sWord Then Goto " & hState.GetNextLabel())
CState.Print("Imbricate(" & CStr($iInclude) & ")") CState.Print("SetIncludeHighlight(" & CStr($iInclude) & ")")
hState.Forward("String.Len(sWord)") hState.Forward("String.Len(sWord)")
CState.Print("Imbricate(0)")
Else Else
CState.IfStartWith($sFrom, hState.GetNextLabel()) CState.IfStartWith($sFrom, hState.GetNextLabel())
CState.Print("Imbricate(" & CStr($iInclude) & ")") CState.Print("SetIncludeHighlight(" & CStr($iInclude) & ")")
hState.Forward(String.Len($sFrom)) hState.Forward(String.Len($sFrom))
CState.Print("Imbricate(0)")
Endif Endif
@ -56,16 +54,16 @@ Public Sub Compile(hState As CState)
If CState.IsRegExp(ByRef $sTo) Then If CState.IsRegExp(ByRef $sTo) Then
CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", True, " & Quote($sTo) & ", aState) Then Return") CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", True, " & Quote($sTo) & ", aState) Then Return")
CState.Print("Imbricate(" & CStr($iInclude) & ")") CState.Print("SetIncludeHighlight(" & CStr($iInclude) & ")")
hState.Forward("String.Len(Match(" & Quote($sTo) & "))") hState.Forward("String.Len(Match(" & Quote($sTo) & "))")
CState.Print("Imbricate(0)") CState.Print("SetIncludeHighlight(Me._IncludeHighlight)")
Else Else
CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", False, " & Quote($sTo) & ", aState) Then Return") CState.Print("If Include(" & CStr($iInclude) & ", " & Quote($sInclude) & ", False, " & Quote($sTo) & ", aState) Then Return")
CState.Print("Imbricate(" & CStr($iInclude) & ")") CState.Print("SetIncludeHighlight(" & CStr($iInclude) & ")")
hState.Forward(String.Len($sTo)) hState.Forward(String.Len($sTo))
CState.Print("Imbricate(0)") CState.Print("SetIncludeHighlight(Me._IncludeHighlight)")
Endif Endif

View file

@ -430,6 +430,12 @@ Static Public Sub GetKeywords() As String
End End
Static Public Sub GetInclude(sInclude As String) As Integer
Try Return $cInclude[sInclude]
End
Static Public Sub AddInclude(sInclude As String) As Integer Static Public Sub AddInclude(sInclude As String) As Integer
Dim iInclude As Integer Dim iInclude As Integer

View file

@ -17,6 +17,7 @@ Public TextAfter As String
Public LengthAdded As Integer Public LengthAdded As Integer
Public Limit As Boolean Public Limit As Boolean
Public Comment As Boolean Public Comment As Boolean
Public _IncludeHighlight As Integer
Property Read Key As String Property Read Key As String
Property Read Name As String Property Read Name As String
@ -24,8 +25,6 @@ Property Read Keywords As String[]
Property Read Colors As String[] Property Read Colors As String[]
Private $iPos As Integer Private $iPos As Integer
Private $iLevel As Integer
Private $aInclude As Byte[]
Private $iInclude As Integer Private $iInclude As Integer
Private $iIndex As Integer Private $iIndex As Integer
Private $hStyle As TextHighlighterStyle Private $hStyle As TextHighlighterStyle
@ -185,7 +184,7 @@ Public Sub _Analyze((Text) As String, (State) As Byte[], Optional (MatchLimit) A
End End
Static Public Sub _Add(aHighlight As Byte[], iState As Byte, iCount As Integer) Public Sub _Add(aHighlight As Byte[], iState As Byte, iCount As Integer)
Dim iMax As Integer Dim iMax As Integer
@ -403,21 +402,10 @@ Catch
End End
Private Sub GetIncludeColor(iInclude As Integer) As Integer
Dim iHue As Integer
iHue = [60, 0, 210, 120, 30, 270][iInclude Mod 6]
Return Color.HSV(iHue, 32, 255)
End
Private Sub HighlightStart(hTheme As TextHighlighterTheme) Private Sub HighlightStart(hTheme As TextHighlighterTheme)
$hTheme = hTheme $hTheme = hTheme
If Not $hTheme Then $hTheme = New TextHighlighterTheme If Not $hTheme Then $hTheme = New TextHighlighterTheme
$iLevel = 0
$aInclude = New Byte[]
$iInclude = 0 $iInclude = 0
$iBackground = Color.Default $iBackground = Color.Default
$aState = New Byte[] $aState = New Byte[]
@ -464,15 +452,15 @@ TRY_AGAIN:
$iIndex += 2 $iIndex += 2
If iLen = 0 Then If iLen = 0 Then
If iState Then ' If iState Then
$aInclude.Push($iInclude) ' $aInclude.Push($iInclude)
$iInclude = iState ' $iInclude = iState
Inc $iLevel ' Inc $iLevel
Else ' Else
$iInclude = $aInclude.Pop() ' $iInclude = $aInclude.Pop()
Dec $iLevel ' Dec $iLevel
Endif ' Endif
$iBackground = GetIncludeColor($iInclude) $iBackground = $hTheme.GetIncludeColor(iState)
Goto TRY_AGAIN Goto TRY_AGAIN
Endif Endif
@ -541,7 +529,7 @@ Public Sub ToHTML(Text As String, Optional Theme As TextHighlighterTheme) As Str
Endif Endif
sStyle = "" sStyle = ""
If $iLevel Then sStyle &= "background-color:" & Color.ToHTML($iBackground) & ";" If $iBackground <> Color.Default Then sStyle &= "background-color:" & Color.ToHTML($iBackground) & ";"
If $hStyle.Color Then sStyle &= "color:" & Color.ToHTML($hStyle.Color) & ";" If $hStyle.Color Then sStyle &= "color:" & Color.ToHTML($hStyle.Color) & ";"
If $hStyle.Dotted Then sStyle &= "text-decoration:underline dotted;" If $hStyle.Dotted Then sStyle &= "text-decoration:underline dotted;"

View file

@ -11,8 +11,10 @@ Static Private $cColorIndex As New Collection
Static Private $cColorsByHighlighter As New Collection Static Private $cColorsByHighlighter As New Collection
Property Settings As String Property Settings As String
Property Read Dark As Boolean
Private $cStyles As New Collection Private $cStyles As New Collection
Private $aStyles As TextHighlighterStyle[]
Static Private $cDefault As New Collection Static Private $cDefault As New Collection
@ -322,25 +324,23 @@ End
Public Sub _GetStyles() As TextHighlighterStyle[] Public Sub _GetStyles() As TextHighlighterStyle[]
Static aStyles As TextHighlighterStyle[]
Dim hStyle As TextHighlighterStyle Dim hStyle As TextHighlighterStyle
If $bStylesModified Or If Not aStyles Then If $bStylesModified Or If Not $aStyles Then
aStyles = New TextHighlighterStyle[] $aStyles = New TextHighlighterStyle[]
aStyles.Add($cStyles["Normal"]) $aStyles.Add($cStyles["Normal"])
For Each $cColorIndex For Each $cColorIndex
hStyle = $cStyles[$cColorIndex.Key] hStyle = $cStyles[$cColorIndex.Key]
If Not hStyle Then hStyle = New TextHighlighterStyle If Not hStyle Then hStyle = New TextHighlighterStyle
aStyles.Add(hStyle) $aStyles.Add(hStyle)
Next Next
$bStylesModified = False $bStylesModified = False
Endif Endif
Return aStyles Return $aStyles
End End
@ -359,3 +359,28 @@ Static Public Sub _GetColorName(sKey As String) As String
Return sName Return sName
End End
Public Sub GetIncludeColor(Include As Integer) As Integer
Dim iHue As Integer
Dim iColor As Integer
If Include = 0 Then Return Color.Default
iHue = [60, 30, 0, 45, 15][(Include - 1) Mod 5]
If Me.Dark Then
iColor = Color.HSV(iHue, 240, 128)
Else
iColor = Color.HSV(iHue, 32, 255)
Endif
Return iColor
End
Private Function Dark_Read() As Boolean
Dim iBg As Integer
iBg = Me["Background"].Color
If Color[iBg].Value < 128 Then Return True
End

View file

@ -105,7 +105,7 @@ Public Sub _Analyze(Text As String, (State) As Byte[], Optional (MatchLimit) As
If Limit And If sSym Begins Limit Then If Limit And If sSym Begins Limit Then
If Not bRewrite Then If Not bRewrite Then
If iPos < Highlight.Positions[I] Then If iPos < Highlight.Positions[I] Then
TextHighlighter._Add(aHighlight, 0, Highlight.Positions[I] - iPos) Me._Add(aHighlight, 0, Highlight.Positions[I] - iPos)
iPos = Highlight.Positions[I] iPos = Highlight.Positions[I]
Endif Endif
Endif Endif
@ -118,12 +118,12 @@ Public Sub _Analyze(Text As String, (State) As Byte[], Optional (MatchLimit) As
Endif Endif
If iPos < Highlight.Positions[I] Then If iPos < Highlight.Positions[I] Then
TextHighlighter._Add(aHighlight, 0, Highlight.Positions[I] - iPos) Me._Add(aHighlight, 0, Highlight.Positions[I] - iPos)
iPos = Highlight.Positions[I] iPos = Highlight.Positions[I]
Endif Endif
iLen = String.Len(sSym) iLen = String.Len(sSym)
TextHighlighter._Add(aHighlight, $cState[Highlight.Types[I]], iLen) Me._Add(aHighlight, $cState[Highlight.Types[I]], iLen)
iPos += iLen iPos += iLen
Next Next
@ -135,7 +135,7 @@ Public Sub _Analyze(Text As String, (State) As Byte[], Optional (MatchLimit) As
Endif Endif
If iPos < iLen Then If iPos < iLen Then
TextHighlighter._Add(aHighlight, 0, iLen - iPos) Me._Add(aHighlight, 0, iLen - iPos)
iPos = iLen iPos = iLen
Endif Endif

View file

@ -41,7 +41,7 @@ Private Sub Init(sText As String)
End End
Private Sub Imbricate(iType As Byte) Private Sub SetIncludeHighlight(iType As Byte)
$aHighlight.Add(iType) $aHighlight.Add(iType)
$aHighlight.Add(0) $aHighlight.Add(0)
@ -51,7 +51,7 @@ End
Private Sub Forward(iState As Byte, Optional N As Integer = 1) Private Sub Forward(iState As Byte, Optional N As Integer = 1)
If $bEof Then Error.Raise("End of file") If $bEof Then Error.Raise("End of file")
TextHighlighter._Add($aHighlight, iState, N) Me._Add($aHighlight, iState, N)
$iPos += N $iPos += N
$bEof = $iPos > $iLen $bEof = $iPos > $iLen
@ -167,15 +167,15 @@ Private Sub Include(iHighlight As Byte, sHighlight As String, bMatch As Boolean,
aState.Remove(0) aState.Remove(0)
' We enter a new imbricated state ' We enter a new imbricated state
SetIncludeHighlight(iHighlight)
If aState.Count = 0 Then If aState.Count = 0 Then
aState.Add(0) aState.Add(0)
$aHighlight.Add(iHighlight)
$aHighlight.Add(0)
Endif Endif
hHighlight = TextHighlighter._Create(sHighlight) hHighlight = TextHighlighter._Create(sHighlight)
hHighlight.TextAfter = "" hHighlight.TextAfter = ""
hHighlight.CanRewrite = Me.CanRewrite hHighlight.CanRewrite = Me.CanRewrite
hHighlight._IncludeHighlight = iHighlight
iPosBefore = $iPos iPosBefore = $iPos
aHighlight = hHighlight._Analyze($sText, aState, bMatch, sLimit, ByRef $iPos) aHighlight = hHighlight._Analyze($sText, aState, bMatch, sLimit, ByRef $iPos)
@ -197,10 +197,12 @@ Private Sub Include(iHighlight As Byte, sHighlight As String, bMatch As Boolean,
If $bEof Then Return True If $bEof Then Return True
' We left the imbricated state ' We left the imbricated state
$aHighlight.Add(0) '$aHighlight.Add(0)
$aHighlight.Add(0) '$aHighlight.Add(0)
aState.Pop() aState.Pop()
SetIncludeHighlight(Me._IncludeHighlight)
End End
Public Sub _Analyze(Text As String, State As Byte[], Optional MatchLimit As Boolean, Limit As String, ByRef iPos As Integer) As Byte[] Public Sub _Analyze(Text As String, State As Byte[], Optional MatchLimit As Boolean, Limit As String, ByRef iPos As Integer) As Byte[]

View file

@ -1,6 +1,6 @@
root{Preprocessor}: root{Preprocessor}:
match "<%/%>" match "<%/%>"
comment.webpage{Preprocessor}: comment.webpage{Documentation}:
from <%-- to --%> from <%-- to --%>
webpage{Preprocessor}: webpage{Preprocessor}:
from << to >> from << to >>