Example41: AutoCompletion

Category: Controls - Scintilla

Date: 02-16-2022

Return to Index


 
 
'This is a fairly long tutorial and set of examples, so get your popcorn,
'sit back, and enjoy!
 
'When a user is moving through source code and the caret is at the end of a function
'whose next argument must be selected from a pre-defined list, Scintilla can be used
'to provide a popup listbox with the allowed selections.  The popup listbox could
'just as easily be used to provide completion of a partially typed word.
 
'Selection of a list item is made by pressing TAB or another character as defined
'with SCI_AutoCSetFillups. Selecting an action also cancels the list.  A displayed
'list can also be cancelled wtih SCI_AutoCCancel.
 
'This capability is called AutoCompletion.  Combining Autocompletion and Call Tips
'can provide capabilities similar to Intellisense, which is found in Microsoft IDE
'products.
 
'A selection from the popup list results in a SCN_AutoSelection notification being
'sent to the container application. On return from the notification, Scintilla
'inserts the selected test unless the action is cancelled within the notification
'response code.
 
'The autocompletion list is automaticaly cancelled if:
' - TAB or other specified character is pressed
' - Sci_AutoCComplete message is sent (same effect as TAB key)
' - caret moves to left of the starting position or of the current word
' - no viable match is found in list (user typing no longer matches a list item)
' - user types a character defined in Sci_AutoCStops
 
'Other options available to support AutoCompletion include:
' - define characters which separate list
' - set list of characters which will auto cancel the list
' - ignore case (case sensitive by default)
' - erase to end of current word (on selection of list item)
' - display images in the list
' - limit number of list items displayed
' - limit width of list items (max number of characters)
' - autoselect from list if list contains only 1 item
 
'Using autocompletion typically requires monitoring each character added to the
'document, using SCN_CharAdded. As each character is added, the content to the left
'of the character is evaluated to determine if the just-typed text (partial word, word,
'or multiple words, depending on the application) should result in the display of the
'popup listbox. That evaluation is performed by the container application.
 
'In PowerBASIC, keywords occur in groups of up to 3 members - single-keyword groups
'double-keyword groups, or triple-keyword groups. Here are a few examples of each:
 
     Join$ <arguments>               'single-keyword group
     Sleep <arguments>               'single-keyword group
 
     Control Redraw <arguments>      'double-keyword group
     Font New <arguments>            'double-keyword group
 
     Dialog Set Text <arguments>     'triple-keyword group
     ListView Get Count <arguments>  'triple-keyword group
 
'The significance of the single/double/triple keyword syntax is that
'autocompletion must always be looking 2 words back in order to know
'what autocompletion list to present to the user.
 
'Then, to determine which argument list to present in a Call Tips window,
'the prior 3 words must be monitored.
 
'In both cases, the search can be limited to a single line since keyword
'groups do not span lines.
 
'PowerBASIC supports are variety of statement structures, consisting of a
'keyword group follow by a syntax list. Here are examples of the various
'syntax formats found in PowerBASIC:
 
    <group> (arg)
    <group> (arg1, arg2, arg3)
    <group> TO arg4, arg5
    <group> (arg1, arg2, arg3 TO arg4, arg5)
    <group> (arg1,arg2) - Step (arg3,arg4), arg5, arg6  'step optional
    <group> (arg1, arg2, Key1|Key2|Key3, arg4, arg5
    <group> (arg1, [ByCmd ]arg2, Key1|Key2|Key3, arg4, TO|At arg5, Call arg6
    <group> arg1, arg2 TO Key1 arg3, arg4           'Dialog Pixels
 
'Some general observations concerning the syntax are:
' - Parentheses are sometimes, but not always optional
' - TO|AT arg4 may be optional, as may be CALL arg6
' - If arg is not displayed, it's bounding separator must be included
' - Key1|Key2|Key3 represent verbatim keyword lists may be found within the arg list
' - The character # may precede some arguments
 
'Understanding the syntax strategies are important to understanding how to provide
'Intellisense-like capabilities by using Call Tips and AutoCompletion.
 
 
http://www.prodevtips.com/2008/05/10/autocomplete-in-wxrubys-scintilla/
 
press Tab to Select
press SCI_AutoCStops character to Cancel (empty by Default)
 
 
SCI_AutoCShow
SCI_AutoCCancel
SCI_AutoCStops       - define characters which Cancel autocompletion (empty by Default)
 
SCI_AutoCGet/SetSeparator  - character(s) that separates list of sorted words
 
Tab                 - Inserts Selected Text (unless Cancelled)
SCI_AutoCComplete   - same as Tab
SCI_AutoCSetFillups - defines characters that work like Tab (empty by Default) but are also inserted
 
SCN_AutoCSelection - called when User Makes a Selection From list
SCI_AutoCCancel    - negates the User selection If called within SCN_AutoCSelection
 
 
'Settings:
SCI_AutoCGet/SetMaxWidth      - character Width of list
SCI_AutoCGet/SetMaxHeight     - # Visible items in list
SCI_Get/SetDropRestOfWord     - Erase rest of word before insertion
SCI_Get/SetAutoHide           - auto Cancel list If there are no viable Matches
SCI_AutoCGet/SetIgnoreCase    - Case sensisitive is the Default action
SCI_AutoCGet/SetChooseSingle  - auto Select Item If is Only Item in list (list Not displayed)
SCI_AutoCGet/SetCancelAtStart - Cancel autocompletion If caret moves before starting Position
 
'List Info
SCI_AutoCSelect      - Selects first Matching word in list (Closes If no Match found)
SCI_AutoCGetCurrent  - Get current selection
SCI_AutoCActive      - is list Shown
SCI_AutoCPosStart    - Postion when SCI_AutoCShow displayed the list
 
'Images:
SCI_RegisterImage
SCI_ClearRegisteredImages
SCI_AutoCGet/'
 
 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Debug Error On     'catch array/pointer errors - OFF in production
#Debug Display On   'display untrapped errors   - OFF in production
#Include "Win32API.inc"
#Include "scintilla_gb.inc"
 
%ID_Sci = 1000
Global hDlg, hSci, hLib As DWord
Global WordList As String
Global ListSeparator As Long
 
Function PBMain() As Long
   hLib = LoadLibrary("SCILEXER.DLL")
   Dialog New Pixels, 0, "Scintilla Example",300,300,300,150, %WS_OverlappedWindow To hDlg
   Control Add "Scintilla", hDlg, %ID_Sci, "", 10,10,180,130, %WS_Child Or %WS_Visible
   Control Handle hDlg, %ID_Sci To hSci     'get handle to Scintilla window
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Local iPos As Long
   Local pNSC As SCNotification Ptr       ' // Scintilla notification messages
   Select Case CB.Msg
      Case %WM_InitDialog
         InitializeScintilla
         PostMessage hSci, %SCI_SetSel, 0,0 'unselect initially
      Case %WM_Notify
         Select Case CB.NmID
            Case %ID_SCi
               pNSC = CB.lParam
               iPos = SendMessage( hSci, %SCI_GetCurrentPos, 0, 0)  'get current position
               Select Case @pNSC.hdr.Code
                  Case %SCN_CHARADDED
                     SendMessage hSci, %SCI_AutoCShow, iPos, StrPTR(WordList)
               End Select
         End Select
   End Select
End Function
 
Sub InitializeScintilla
   Local txt As String
   txt = "If x = 2 Then" + $CrLf + "   'do nothing" + $CrLf
   txt = txt + "Else" + $CrLf + "   x = 0" + $CrLf + "End If" + Chr$(0)
   SendMessage hSci, %SCI_SetText, 0, StrPTR(txt)
   SendMessage hSci, %SCI_SetMarginWidthN, 0, 20
   SendMessage hSci, %SCI_GoToPos, 2, 0    'no selection (set anchor=curpos=iPos)
   Control Set Focus hDlg, %ID_Sci
   ListSeparator = Asc(":")   'not use space because PowerBASIC uses 3-word keyword groups
   SendMessage hSci, %SCI_AutoCSetSeparator, ListSeparator, 0  'set separator
   WordList = "End Class :End Function :End If :End Interface :End Macro :End Method "
   WordList = WordList + ":End Property :End Sub :End Type :End Union :End Try " + $Nul
End Sub
 
'gbs_00659
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm