The IDE's syntax parser (the one that does the coloring) is available as a dll in the purebasic package in the SDK folder. This make it easy to add coloring to a project. A basic example is provided below. Features like auto completion will be more work as you need to scan the source code yourself to extract tokens such as variables and functions for the completion list.
Code: Select all
; Color values returned in the Dll callback
;
Enumeration
#SYNTAX_Text
#SYNTAX_Keyword
#SYNTAX_Comment
#SYNTAX_Constant
#SYNTAX_String
#SYNTAX_Function
#SYNTAX_Asm
#SYNTAX_Operator
#SYNTAX_Structure
#SYNTAX_Number
#SYNTAX_Pointer
#SYNTAX_Separator
#SYNTAX_Label
#SYNTAX_Module
EndEnumeration
; Define protype for the dll function
;
Prototype SyntaxHighlight(*Buffer, Length, *Callback, Asm)
Global SyntaxHighlight.SyntaxHighlight
; Callback for the syntax parser
;
Procedure ColorCallback(*Position, Length, Color)
ScintillaSendMessage(0, #SCI_SETSTYLING, Length, Color)
EndProcedure
; Callback for scintilla events
;
Procedure ScintillaCallback(Gadget, *scinotify.SCNotification)
Protected LastStyled, Range.TextRange
; This event indicates that new coloring is needed. The #SCI_GETENDSTYLED message and *scinotify\position indicate the range to color
If *scinotify\nmhdr\code = #SCN_STYLENEEDED
; calculate the range to color
; always start coloring at the line start
LastStyled = ScintillaSendMessage(Gadget, #SCI_GETENDSTYLED)
Range\chrg\cpMin = ScintillaSendMessage(Gadget, #SCI_POSITIONFROMLINE, ScintillaSendMessage(Gadget, #SCI_LINEFROMPOSITION, LastStyled))
Range\chrg\cpMax = *scinotify\position
Range\lpstrText = AllocateMemory(Range\chrg\cpMax - Range\chrg\cpMin + 1)
If Range\lpstrText
; retrieve the text range
ScintillaSendMessage(Gadget, #SCI_GETTEXTRANGE, 0, @Range)
; start coloring
ScintillaSendMessage(Gadget, #SCI_STARTSTYLING, Range\chrg\cpMin, $FF)
; call the parser function in the dll
; the callback above will apply the colors to the returned tokens
SyntaxHighlight(Range\lpstrText, Range\chrg\cpMax - Range\chrg\cpMin, @ColorCallback(), #False)
FreeMemory(Range\lpstrText)
EndIf
EndIf
EndProcedure
If InitScintilla() And OpenLibrary(0, #PB_Compiler_Home + "SDK\Syntax Highlighting\SyntaxHilighting.dll")
; get the syntax parser function
SyntaxHighlight = GetFunction(0, "SyntaxHighlight")
; create window and gadget
OpenWindow(0, 0, 0, 640, 480, "Tiny Editor", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget)
ScintillaGadget(0, 0, 0, 640, 480, @ScintillaCallback())
; Important: tell the gadget to send the #SCN_STYLENEEDED notification to the callback if coloring is needed
ScintillaSendMessage(0, #SCI_SETLEXER, #SCLEX_CONTAINER)
; Enable line numbers
ScintillaSendMessage(0, #SCI_SETMARGINTYPEN, 0, #SC_MARGIN_NUMBER)
; Set common style info
Define *FontName = AllocateMemory(StringByteLength("Courier New", #PB_Ascii) + 1)
If *FontName
PokeS(*FontName, "Courier New", -1, #PB_Ascii)
ScintillaSendMessage(0, #SCI_STYLESETFONT, #STYLE_DEFAULT, *FontName)
FreeMemory(*FontName)
EndIf
ScintillaSendMessage(0, #SCI_STYLESETSIZE, #STYLE_DEFAULT, 12)
ScintillaSendMessage(0, #SCI_STYLESETBACK, #SCI_STYLESETFORE, 0)
ScintillaSendMessage(0, #SCI_STYLESETBACK, #STYLE_DEFAULT, $DFFFFF)
ScintillaSendMessage(0, #SCI_STYLECLEARALL)
; Set individual colors
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Text, 0)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Keyword, $666600)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Comment, $AAAA00)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Constant, $724B92)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_String, $FF8000)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Function, $666600)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Asm, $DFFFFF)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Operator, 0)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Structure, 0)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Number, 0)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Pointer, 0)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Separator, 0)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Label, 0)
ScintillaSendMessage(0, #SCI_STYLESETFORE, #SYNTAX_Module, 0)
ScintillaSendMessage(0, #SCI_STYLESETBOLD, #SYNTAX_Keyword, #True)
; Minimalistic event loop
Define Event
Repeat
Event = WaitWindowEvent()
If Event = #PB_Event_SizeWindow
ResizeGadget(0, 0, 0, WindowWidth(0), WindowHeight(0))
EndIf
Until Event = #PB_Event_CloseWindow
Else
MessageRequester("Error", "Cannot load scintilla or parser dll")
EndIf