Merge branch 'sh.highlight' into 'master'

Sh.highlight: new added sh (shell) to TextEditor

See merge request gambas/gambas!156
This commit is contained in:
Benoît Minisini 2020-10-27 21:58:52 +00:00
commit 6e6a86b23c
3 changed files with 314 additions and 2 deletions

View file

@ -0,0 +1,298 @@
' Gambas class file
Inherits TextHighlighter
Public Const Name As String = "sh"
Private Const IDENT_CAR As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_"
Private Const DIGIT_CAR As String = "0123456789"
Static Private $cKeyword As New Collection
Static Private $cOperator As New Collection
Static Private $cType As New Collection
Static Private $aKeywords As String[]
Static Public Sub _init()
Dim sStr As String
$aKeywords = New String[]
For Each sStr In ["function", "do", "else", "test", "for", "to", "in", "fi", "if", "elif", "then", "return", "exit",
"while", "until", "done", "break", "continue", "select", "case", "esac"]
$cKeyword[sStr] = 0
$aKeywords.Add(sStr)
Next
For Each sStr In ["false", "null" "true"]
$cKeyword[sStr] = 1
$aKeywords.Add(sStr)
Next
For Each sStr In ["{", "}", "$", "#", "[", "]", "-gt", "-lt", "-ge", "-ne", "-le", "-eq", "<<<",
".", "+", "<<", "=", ">>", "==", "-", ">>", "~", "+=", "(", ",", "!=", "*", "-=", "&=", ";;",
")", "<", "%", "&", "];", "@", "!", ";", ">", "|", "?", "^", "\\", ":", "<=", "/", "`"]
$cOperator[sStr] = True
Next
' As there is no datatype defining in sh we can use Type for popular commands.
For Each sStr In ["echo", "read", "cd", "which", "rm", "cp", "mv", "rmdir", "cat",
"grep", "awk", "tr", "sed", "sleep", "clear", "sudo", "su", "source", "eval", "export",
"time", "date", "pwd", "set", "unset", "chown", "chmod", "exec", "alias", "unalias",
"bg", "bind", "builtin", "caller", "command", "compgen", "complete", "compopt", "declare",
"dirs", "disown", "enable", "fc", "fg", "getopts", "hash", "help", "history", "jobs", "kill",
"let", "local", "logout", "mapfile", "popd", "printf", "pushd", "readarray", "readonly",
"shift", "shopt", "suspend", "test", "times", "trap", "type", "typeset", "ulimit", "umask" "wait"]
$cType[sStr] = True
$aKeywords.Add(sStr)
Next
End
Public Sub GetKeywords() As String[]
Return $aKeywords
End
Public Sub RunWith(sText As String, cKeyword As Collection, cOperator As Collection, cType As Collection)
Dim iState As Integer
Dim iTag As Integer
Dim iLenText As Integer
Dim iPos, iSavePos As Integer
Dim sCar As String
Dim bSingleQuoteString As Boolean
Dim sIdent As String
Dim iPosType, iPosIdent, iPosAfterIdent As Integer
Dim bLastExpr As Boolean
iState = TextHighlighter.State
iTag = TextHighlighter.Tag
iLenText = String.Len(sText)
bLastExpr = True
If iState = Highlight.Operator Then bLastExpr = False
If Left(LTrim(sText)) = "#" Or If iState = Highlight.Comment Then
TextHighlighter.Add(Highlight.Comment, iLenText)
Else
For iPos = 1 To iLenText
If iState = Highlight.Normal And If iTag <> Highlight.String Then
sCar = String.Mid$(sText, iPos, 1)
If sCar = "\"" Then
iState = Highlight.String
iTag = False
bSingleQuoteString = False
Else If sCar = "'" Then
iState = Highlight.String
bSingleQuoteString = True
iTag = True
Else If Asc(sCar) <= 32 Then
Else If InStr(Trim(sText), "function") = 1 Then
TextHighlighter.Add(Highlight.Keyword, 9)
TextHighlighter.Add(Highlight.Function, RInStr(sText, "(") - 10)
Break
Else If sCar = "#" Then
sCar = String.Mid$(sText, iPos - 1, 1)
If sCar <> "{" Then
TextHighlighter.Add(Highlight.Comment, iLenText)
Break
Else
TextHighlighter.Add(Highlight.Operator, 1)
Continue
Endif
Else If IsDigit(sCar) Then
sIdent = sCar
sCar = String.Mid$(sText, iPos + 1, 1)
If sCar = "x" Or If sCar = "X" Then
sIdent &= sCar
Inc iPos
While iPos < iLenText
Inc iPos
sCar = String.Mid$(sText, iPos, 1)
If Not IsHexa(sCar) Then
Dec iPos
Break
Endif
sIdent &= sCar
Wend
Else
While iPos < iLenText
Inc iPos
sCar = String.Mid$(sText, iPos, 1)
If InStr(DIGIT_CAR, sCar) = 0 Then
Dec iPos
Break
Endif
sIdent &= sCar
Wend
Endif
TextHighlighter.Add(Highlight.Number, String.Len(sIdent))
iState = Highlight.Normal
bLastExpr = True
Continue
Else If Len(sCar) >= 2 Or If InStr(IDENT_CAR, sCar) Then
sIdent = sCar
iSavePos = iPos
While iPos < iLenText
Inc iPos
sCar = String.Mid$(sText, iPos, 1)
If Len(sCar) = 1 And If Not IsDigit(sCar) And If InStr(IDENT_CAR, sCar) = 0 Then
Dec iPos
Break
Endif
sIdent &= sCar
Wend
If cKeyword.Exist(sIdent) Then
iState = Highlight.Keyword
If cKeyword[sIdent] = 1 Then bLastExpr = True
Else If InStr(RTrim(sText), "()", String.Len(sText) - 2) Then
If Not InStr(RTrim(sText), "=", String.Len(sText) - 3) Then
iState = Highlight.Function
bLastExpr = True
Else
iState = Highlight.Symbol
bLastExpr = True
Endif
Else If InStr(RTrim(sText), "() {", String.Len(sText) - 4) Then
iState = Highlight.Function
bLastExpr = True
Else If cType.Exist(sIdent) Then
iState = Highlight.Datatype
bLastExpr = True
If iPosType = 0 Then iPosType = iSavePos
Else
iState = Highlight.Symbol
If iPosAfterIdent = 0 Then
iPosIdent = iSavePos
iPosAfterIdent = iSavePos + String.Len(sIdent)
Endif
bLastExpr = True
Endif
TextHighlighter.Add(iState, String.Len(sIdent))
iState = Highlight.Normal
Continue
Else
sIdent = sCar
While iPos < iLenText
If cOperator.Exist(sIdent) Then
If sIdent = "-" Then
sCar = String.Mid$(sText, iPos + 1, 2)
Select sCar
Case "eq", "ne", "gt", "lt", "ge", "le", "o "
TextHighlighter.Add(Highlight.Operator, String.Len(sCar) + 1)
iPos += String.Len(sCar) + 1
Break
End Select
Endif
Break
Endif
Inc iPos
sCar = String.Mid$(sText, iPos, 1)
If IsLetter(sCar) Or If IsDigit(sCar) Or If IsBlank(sCar) Then
Dec iPos
Break
Endif
sIdent &= sCar
Wend
If cOperator.Exist(sIdent) Then
iState = Highlight.Operator
Else
iState = Highlight.Error
Endif
TextHighlighter.Add(iState, String.Len(sIdent))
iState = Highlight.Normal
bLastExpr = False
'Dec iPos
Continue
Endif
Else If iState = Highlight.String Then
sCar = String.Mid$(sText, iPos, 1)
If sCar = "\\" Then
sCar = String.Mid$(sText, iPos + 1, 1)
If sCar <> "'" And If sCar <> "\"" Then
TextHighlighter.Add(Highlight.Escape, 2)
Inc iPos
Else
TextHighlighter.Add(Highlight.Escape, 1)
If sCar = "\"" Then Inc ipos
Endif
'Inc iPos
Continue
Else If sCar = "\"" And If Not iTag Then
TextHighlighter.Add(iState)
iState = Highlight.Normal
iTag = iState
bLastExpr = True
Continue
Else If sCar = "'" And If iTag Then
TextHighlighter.Add(iState)
iState = Highlight.Normal
iTag = iState
bLastExpr = True
Continue
Endif
Endif
TextHighlighter.Add(iState)
Next
If iState <> Highlight.Comment And If iState <> Highlight.Help And If iState <> Highlight.String Then
iState = Highlight.Normal
Endif
Endif
TextHighlighter.Limit = False
' If iPosIdent >= 1 And If Not IsSpace(Left(sText)) Then
' If InStr(String.Mid$(sText, iPosAfterIdent), "(") Then
' If RInStr(sText, "\\") = 0 Then
' TextHighlighter.Limit = True
' Endif
' Endif
' Endif
TextHighlighter.State = iState
TextHighlighter.Tag = iTag
End
Public Sub Run(Text As String)
RunWith(Text, $cKeyword, $cOperator, $cType)
End

View file

@ -4,7 +4,7 @@ Export
Inherits UserControl
Public Const _Properties As String = "*,Border=True,ShowPreview,ScrollBar{Scroll.*}=Both,Highlight{None;Custom;C;CPlusPlus;CSS;Diff;Gambas;HTML;JavaScript;SQL;WebPage}=None,Mode{None;Custom;C;CPlusPlus;CSS;Diff;Gambas;HTML;JavaScript;SQL;WebPage}=None,ReadOnly,TabSize{Range:2;64}=2,TabIndent,Wrap,ShowPosition,ShowLimit,ShowCurrent,ShowLineNumber,ShowModified,ShowBraces,ShowIcon,ShowExpand,ShowCursor=True,ShowSpaces,ShowIndent,CloseBraces,CloseStrings"
Public Const _Properties As String = "*,Border=True,ShowPreview,ScrollBar{Scroll.*}=Both,Highlight{None;Custom;C;CPlusPlus;CSS;Diff;Gambas;HTML;JavaScript;SQL;WebPage;Sh}=None,Mode{None;Custom;C;CPlusPlus;CSS;Diff;Gambas;HTML;JavaScript;SQL;WebPage;Sh}=None,ReadOnly,TabSize{Range:2;64}=2,TabIndent,Wrap,ShowPosition,ShowLimit,ShowCurrent,ShowLineNumber,ShowModified,ShowBraces,ShowIcon,ShowExpand,ShowCursor=True,ShowSpaces,ShowIndent,CloseBraces,CloseStrings"
Public Const _DrawWith As String = "-"
Public Const _DefaultEvent As String = "Change"
Public Const _Similar As String = "TextArea"
@ -392,7 +392,7 @@ Private Sub Init()
$bShowCursor = True
GotoCenter(0, 0)
Refresh
End

View file

@ -0,0 +1,14 @@
' Gambas class file
Inherits TextEditorMode
Static Public Const BRACES_OPEN As String = "([{\"'`"
Static Public Const BRACES_CLOSE As String = ")]}\"'`"
Static Public Const STRING_DELIM As String = "\"'"
Public Sub OnKeyPress(hEditor As TextEditor) As Boolean
Return Super.OnKeyPress(hEditor)
End