Scintilla / GoScintilla - Get Hover Word

Share your advanced PureBasic knowledge/code with the community.
User avatar
Innesoft
Enthusiast
Enthusiast
Posts: 105
Joined: Mon Jan 18, 2010 10:30 am
Location: UK
Contact:

Scintilla / GoScintilla - Get Hover Word

Post by Innesoft »

I wrote an extra function for GoScintilla by Stephen Rodriguez (srod) and thought I'd share it since it's quite useful.
Just stick it into GoScintilla.pbi to use it.

Returns the word currently hovered over by the cursor, which can be used for F1-> contextual help, or whatever. If you're writing an IDE, this can be used to get help on commands.

Usage:
word$ = GOSCI_GetHoverText(id)

Code: Select all

;/////////////////////////////////////////////////////////////////////////////////
;The following function returns the word currently under the cursor.
Procedure.s GOSCI_GetHoverText(id)
  Protected text$
  If IsGadget(id) And GadgetType(id) = #PB_GadgetType_Scintilla
    curPos = ScintillaSendMessage(id, #SCI_GETCURRENTPOS)
    linePos = ScintillaSendMessage(id, #SCI_LINEFROMPOSITION,curPos)
    colPos = ScintillaSendMessage(id, #SCI_GETCOLUMN,curPos)
    
    text$=GOSCI_GetLineText(id, linePos)
    word$=""
    ;get the start of the word
    colPos=colPos+1
    stPos.i=1
    nonalpha.i=0
    For a.i=colPos To 1 Step-1
      m.c=Asc(Mid(text$,a.i,1))
      Select m.c
        Case 'a' To 'z', 'A' To 'Z'
          If nonalpha.i=0
            stPos.i=a.i
          EndIf
        Default
          nonalpha.i=1
      EndSelect
    Next
    ;get word
    For a.i=stPos.i To Len(text$)
      m.c=Asc(Mid(text$,a.i,1))
      Select m.c
        Case 'a' To 'z', 'A' To 'Z'
          word$=word$+Chr(m.c)
        Default
          If Trim(word$)<>""
            ProcedureReturn word$
          EndIf
      EndSelect
    Next
  EndIf
  ProcedureReturn word$
EndProcedure
;/////////////////////////////////////////////////////////////////////////////////
Innesoft - The Software Marketplace - Innesoft Blog
» Applications, Educational Software, Casual Games
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: Scintilla / GoScintilla - Get Hover Word

Post by Tenaja »

This is a little quicker way to do it, and it uses the Gosci settings for what defines a "word". I just used it for putting text into the clipboard, but you can easily modify the last line if you need it in a string.

Code: Select all

			pos = SCI_GetCurrentPos(ActiveSci)
			WordStart = SCI_WordStartPosition(ActiveSci, pos, #True) ;bool onlyWordCharacters)
			WordEnd = SCI_WordEndPosition(ActiveSci, pos,#True); bool onlyWordCharacters)
; To put it in the clipboard:
			SendSci(ActiveSci, #SCI_COPYRANGE, WordStart, WordEnd) 
; To put it in a string, convert this to pb:
; SCI_GETTEXTRANGE(<unused>, Sci_TextRange *tr)
Post Reply