Scintilla autocompletion

Share your advanced PureBasic knowledge/code with the community.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Scintilla autocompletion

Post by eddy »

Code: Select all

EnableExplicit
InitScintilla()

Define sciFont.s, hdr$, hdrLen, memBuffer

Procedure MakeUTF8Text(text.s)
   Static buffer.s
   buffer=Space(StringByteLength(text, #PB_UTF8) + 1)
   PokeS(@buffer, text, -1, #PB_UTF8)
   ProcedureReturn @buffer
EndProcedure

ProcedureDLL AutoCompletionScintillaCB(Gadget, *scinotify.SCNotification)
   With *scinotify
      
      If \nmhdr\code=#SCN_CHARADDED         
         Select \ch
            Case '\', 'a' To 'z'
               ScintillaSendMessage(0, #SCI_AUTOCSETMAXHEIGHT, 600)
               ScintillaSendMessage(0, #SCI_AUTOCSETMAXWIDTH, 400)
               ScintillaSendMessage(0, #SCI_AUTOCSETAUTOHIDE, #False)    ;True => auto-hide if no match
               ScintillaSendMessage(0, #SCI_AUTOCSETCHOOSESINGLE, #True) ;True => auto-select if only one match
               ScintillaSendMessage(0, #SCI_AUTOCSETIGNORECASE, #True)   ;True => perform a caseless search ('a' = 'A')
               Protected *autocompAutoCanceledByCharacters=MakeUTF8Text(";,'")
               ScintillaSendMessage(0, #SCI_AUTOCSTOPS, 0, *autocompAutoCanceledByCharacters)
               Protected *autocompAutoSelectedByCharacters=MakeUTF8Text("|({ ")
               ScintillaSendMessage(0, #SCI_AUTOCSETFILLUPS, 0, *autocompAutoSelectedByCharacters)
               Protected autocompSeparatorChar=' ', sep$=Chr(autocompSeparatorChar)
               Protected autocompTypeChar=':', typ$=Chr(autocompTypeChar)
               ScintillaSendMessage(0, #SCI_AUTOCSETSEPARATOR, autocompSeparatorChar)
               ScintillaSendMessage(0, #SCI_AUTOCSETTYPESEPARATOR, autocompTypeChar)
               Protected autocompList.s="\v" + typ$ + 0 + sep$ + ;type=0 (just for test)
                                        "\r" + typ$ + 1 + sep$ + ;type=1 (just for test)
                                        "\n" + sep$ + 
                                        "\t" + sep$ + 
                                        "\" + #DQUOTE$ + sep$ + 
                                        "\f" + sep$ + 
                                        "\no" + sep$ + 
                                        "\ok"
               ScintillaSendMessage(0, #SCI_AUTOCSETORDER, #SC_ORDER_PERFORMSORT) ;<= because my autocompletion list is disordered
               
               ;search the closest word beginning with '\' character from the cursor position
               Protected currentPos=ScintillaSendMessage(0, #SCI_GETCURRENTPOS)
               Protected closestWordStart=ScintillaSendMessage(0, #SCI_WORDSTARTPOSITION, currentPos, 1)
               Protected autocompSearchLength=currentPos-closestWordStart
               If ScintillaSendMessage(0, #SCI_GETCHARAT, closestWordStart-1)='\'
                  ;search '\xxxxxx'
                  autocompSearchLength + 1
               EndIf               
               
               Debug autocompSearchLength
               ScintillaSendMessage(0, #SCI_AUTOCSHOW, autocompSearchLength, MakeUTF8Text(autocompList))
         EndSelect
      EndIf      
   EndWith
EndProcedure

hdr$=""
hdr$ + "----------------------------------------" + #CR$
hdr$ + "----                                ----" + #CR$
hdr$ + "----    Type escape character       ----" + #CR$
hdr$ + "----    \r \n \t \" + #DQUOTE$ + "                 ----" + #CR$
hdr$ + "----    to display auto-completion   ----" + #CR$
hdr$ + "----------------------------------------" + #CR$
hdrLen=StringByteLength(hdr$, #PB_UTF8)


OpenWindow(0, 0, 0, 600, 700, "Block me lines!", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ScintillaGadget(0, 0, 0, WindowWidth(0), WindowHeight(0), @AutoCompletionScintillaCB())
ScintillaSendMessage(0, #SCI_STYLESETFONT, #STYLE_DEFAULT, MakeUTF8Text("Courier New"))
ScintillaSendMessage(0, #SCI_SETWRAPMODE, #SC_WRAP_WHITESPACE)
ScintillaSendMessage(0, #SCI_SETCODEPAGE, #SC_CP_UTF8)

;set CHARACTER settings (autcompletion, selection and other features use these settings)
ScintillaSendMessage(0, #SCI_SETWORDCHARS, 0, MakeUTF8Text("abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"))
;ScintillaSendMessage(0, #SCI_SETWHITESPACECHARS, 0, MakeUTF8Text(" " + #TAB$ + #CR$ + #LF$))  ;<= Uncomment this line if you want change default values
;ScintillaSendMessage(0, #SCI_SETPUNCTUATIONCHARS, 0, MakeUTF8Text("(){}[];:,'"+#DQUOTE$))     ;<= Uncomment this line if you want change default values

ScintillaSendMessage(0, #SCI_SETTEXT, 0, MakeUTF8Text(hdr$))
ScintillaSendMessage(0, #SCI_GOTOPOS, hdrLen)
SetActiveGadget(0)

Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool