the following is based on the block-comment demo, but for simplicity (so as not to confuse the issue too much) I have removed the block-comment styling code.
Instead, only lines (or part lines) beginning with // are commented. A part line will be commented only if the // occurs at the beginning of a line or the preceeding character is either whitespace (tab or space) or a ; character.
Code: Select all
IncludePath "..\..\..\"
XIncludeFile "GoScintilla.pbi"
Declare.i myLineStyler(id, *utf8Buffer.ASCII, numUtf8Bytes, currentLine, startLine, originalEndLine)
;Initialise the Scintilla library for Windows.
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
InitScintilla()
CompilerEndIf
If OpenWindow(0, 100, 200, 600, 600, "GoScintilla demo!", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_SizeGadget)
RemoveKeyboardShortcut(0, #PB_Shortcut_Tab) ;Required for the tab key to function correctly when the Scintilla control has the focus.
;Create our Scintilla control. Note that we do not specify a callback; this is optional for GoSctintilla.
GOSCI_Create(1, 10, 10, 580, 580, 0, #GOSCI_AUTOSIZELINENUMBERSMARGIN|#GOSCI_ALLOWCODEFOLDING)
;Set the padding added to the width of the line-number margin.
GOSCI_SetAttribute(1, #GOSCI_LINENUMBERAUTOSIZEPADDING, 10)
;Set folding symbols margin width.
GOSCI_SetMarginWidth(1, #GOSCI_MARGINFOLDINGSYMBOLS, 24)
;Set the back color of the line containing the caret.
GOSCI_SetColor(1, #GOSCI_CARETLINEBACKCOLOR, $B4FFFF)
;Set font.
GOSCI_SetFont(1, "Courier New", 10)
;Set tabs. Here we use a 'hard' tab in which a tab character is physically inserted. Set the 3rd (optional) parameter to 1 to use soft-tabs.
GOSCI_SetTabs(1, 2)
;Set styles for our syntax highlighting.
;=======================================
;First define some constants to identify our various styles.
;You can name these as we wish.
Enumeration
#STYLES_COMMANDS = 1
#STYLES_COMMENTS
#STYLES_COMPILER
#STYLES_LITERALSTRINGS
#STYLES_NUMBERS
#STYLES_FUNCTIONS
#STYLES_FIELDS
EndEnumeration
;Set individual styles for commands.
GOSCI_SetStyleFont(1, #STYLES_COMMANDS, "", -1, #PB_Font_Bold)
GOSCI_SetStyleColors(1, #STYLES_COMMANDS, $800000) ;We have omitted the optional back color.
;Set individual styles for comments.
GOSCI_SetStyleFont(1, #STYLES_COMMENTS, "", -1, #PB_Font_Italic)
GOSCI_SetStyleColors(1, #STYLES_COMMENTS, $006400) ;We have omitted the optional back color.
;Set individual styles for compiler directives etc..
GOSCI_SetStyleColors(1, #STYLES_COMPILER, $2193DE) ;We have omitted the optional back color.
;Set individual styles for literal strings.
GOSCI_SetStyleColors(1, #STYLES_LITERALSTRINGS, #Gray) ;We have omitted the optional back color.
;Set individual styles for numbers.
GOSCI_SetStyleColors(1, #STYLES_NUMBERS, #Red) ;We have omitted the optional back color.
;Set individual styles for functions.
GOSCI_SetStyleColors(1, #STYLES_FUNCTIONS, #Blue) ;We have omitted the optional back color.
;Set individual styles for fields.
GOSCI_SetStyleColors(1, #STYLES_FIELDS, $2730D8) ;We have omitted the optional back color.
;Set keywords for our syntax highlighting.
;=========================================
;First some commands.
GOSCI_AddKeywords(1, "auto bool break bstr case char const continue date default do double else enum", #STYLES_COMMANDS)
GOSCI_AddKeywords(1, "extern float for goto if int long lpwstr lpvoid register return short signed", #STYLES_COMMANDS)
GOSCI_AddKeywords(1, "sizeof static struct switch typedef ulong union unsigned variant vartype void volatile while", #STYLES_COMMANDS)
;Now set up quotes to denote literal strings. Note the space separating the pair of quotes from the pait of ' symbols.
GOSCI_AddKeywords(1, Chr(34) + Chr(34) + " ''", #STYLES_LITERALSTRINGS, #GOSCI_DELIMITBETWEEN)
;Now set up a # symbol to denote a compiler directive.
GOSCI_AddKeywords(1, "#", #STYLES_COMPILER, #GOSCI_LEFTDELIMITWITHOUTWHITESPACE)
;Now set up a ( symbol to denote a function.
GOSCI_AddKeywords(1, "(", #STYLES_FUNCTIONS, #GOSCI_RIGHTDELIMITWITHWHITESPACE)
;We arrange for a ) symbol to match the coloring of the ( symbol.
GOSCI_AddKeywords(1, ")", #STYLES_FUNCTIONS)
;Now set up a . symbol to delimit structure members.
GOSCI_AddKeywords(1, ".", #STYLES_FIELDS, #GOSCI_LEFTDELIMITWITHWHITESPACE|#GOSCI_RIGHTDELIMITWITHWHITESPACE)
;Add some folding keywords.
GOSCI_AddKeywords(1, "{", #STYLES_COMMANDS, #GOSCI_OPENFOLDKEYWORD)
GOSCI_AddKeywords(1, "}", #STYLES_COMMANDS, #GOSCI_CLOSEFOLDKEYWORD)
;Additional lexer options.
;=========================
;The lexer needs to know what separator characters we are using.
GOSCI_SetLexerOption(1, #GOSCI_LEXEROPTION_SEPARATORSYMBOLS, @"=+-*/%[],{}&;|!>()") ;You would use GOSCI_AddKeywords() to set a style for some of these if required.
;We can also set a style for numbers.
GOSCI_SetLexerOption(1, #GOSCI_LEXEROPTION_NUMBERSSTYLEINDEX, #STYLES_NUMBERS)
;Set our user-defined line styling function.
;===========================================
GOSCI_SetLineStylingFunction(1, @MyLineStyler())
Repeat
eventID = WaitWindowEvent()
Select eventID
Case #PB_Event_SizeWindow
ResizeGadget(1, #PB_Ignore, #PB_Ignore, WindowWidth(0)-20, WindowHeight(0)-20)
Case #PB_Event_Gadget
Select EventGadget()
EndSelect
EndSelect
Until eventID = #PB_Event_CloseWindow
;Free the Scintilla gadget.
;This needs explicitly calling in order to free resources used by GoScintilla.
GOSCI_Free(1)
EndIf
;/////////////////////////////////////////////////////////////////////////////////
;The following is our user-defined line-styling function, called whenever GoScintilla is called upon to style lines.
;Here we look out for // line comment markers and, if encountered, comment the remainder of the line IF the preceding character was a
;whitespace character or a ; or the beginning of the line.
Procedure.i myLineStyler(id, *utf8Buffer.ASCII, numUtf8Bytes, currentLine, startLine, originalEndLine)
Protected result = #GOSCI_STYLELINESASREQUIRED, numBytesStyled, *ptrAscii.ASCII, blnLastSymbolAValidStarter = #True
While numUtf8Bytes
numBytesStyled = 0
*ptrAscii = *utf8Buffer
If blnLastSymbolAValidStarter And *ptrAscii\a = '/' And numUtf8Bytes > 1
*ptrAscii + 1
If *ptrAscii\a = '/' ;We have a single line comment and so we style the remainder of this line (excluding EOL characters) appropriately.
numBytesStyled = numUtf8Bytes
;Do not apply the comment style to EOL characters as this can cause problems.
*ptrAscii + numUtf8Bytes - 1
While *ptrAscii\a = #LF Or *ptrAscii\a = #CR
*ptrAscii - 1
numBytesStyled - 1
Wend
ScintillaSendMessage(id, #SCI_SETSTYLING, numBytesStyled, #STYLES_COMMENTS)
EndIf
EndIf
If numBytesStyled = 0
If *ptrAscii\a = 9 Or *ptrAscii\a = 32 Or *ptrAscii\a = ';'
blnLastSymbolAValidStarter = #True
Else
blnLastSymbolAValidStarter = #False
EndIf
numBytesStyled = GOSCI_StyleNextSymbol(id, *utf8Buffer, numUtf8Bytes)
EndIf
numUtf8Bytes - numBytesStyled
*utf8Buffer + numBytesStyled
Wend
ProcedureReturn result
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////
The code is probably not very efficient because I hacked it up very quickly.

I hope this helps?