本帖最后由 tubaba 于 2017-5-8 13:25 编辑
调用Scintilla的SCI_GETSELTEXT函数,如果返回空,循環读取光标左右字符,直到单词边界,SCI_GETSELTEXT(<unused>, char *text NUL-terminated) → int
This copies the currently selected text and a terminating 0 byte to the text buffer. The buffer size should be determined by calling with a NULL pointer for the text argument SCI_GETSELTEXT(0,0). This allows for rectangular and discontiguous selections as well as simple selections. See Multiple Selection for information on how multiple and rectangular selections and virtual space are copied.
LUA函数原型function AutoItTools:GetWord2()
local word = editor:GetSelText()
if word == "" then
-- When on an ( or space, go to the start of the previous word.
if editor.CharAt[editor.CurrentPos] == 40 or editor.CharAt[editor.CurrentPos] == 32 then
editor:WordLeft()
end
-- Cache the style as it's used numerous times.
local style = editor.StyleAt[editor.CurrentPos]
-- The start and end of the text range.
local from = editor:WordStartPosition(editor.CurrentPos)
local to = editor:WordEndPosition(editor.CurrentPos)
-- Use a variable to shorten the for loop code.
local line = editor:LineFromPosition(editor.CurrentPos)
-- Caret is on a function.
if style == SCE_AU3_FUNCTION or style == SCE_AU3_DEFAULT or style == SCE_AU3_UDF then
-- A counter of the number of opening brackets encountered.
local brackets = 0
-- A flag set to true if an opening bracket is encountered.
local found = false
-- Iterate the line looking for the end of the function call.
for i = editor.CurrentPos, editor.LineEndPosition[line] do
-- Make sure we don't count brackets in strings.
if editor.StyleAt[i] ~= SCE_AU3_STRING then
-- Found an opening bracket, increment counter and set flag.
if editor.CharAt[i] == 40 then
brackets = brackets + 1
found = true
end
-- Found a closing bracket, decrement counter.
if editor.CharAt[i] == 41 then
brackets = brackets - 1
end
end
-- We found a bracket and we found the end, set to and break.
if found and brackets == 0 then
to = i + 1
break
end
end
-- If we didn't find any brackets, just return the simple GetWord().
if not found then
return self:GetWord()
end
-- Caret is in a string.
elseif style == SCE_AU3_STRING then
-- Find the start of the string. To do this, we iterate backwards
-- to the indentation.
for i = editor.CurrentPos, editor.LineIndentPosition[line] - 1, -1 do
if editor.StyleAt[i] ~= SCE_AU3_STRING then
-- We have to add 1 or we'll pick up the non-string
-- character as well.
from = i + 1
break
end
end
-- Find the end of the string. To do this, we iterate forwards to
-- the end of the string.
for i = editor.CurrentPos, editor.LineEndPosition[line] do
if editor.StyleAt[i] ~= SCE_AU3_STRING then
to = i
break
end
end
end
-- Get the text range.
word = editor:textrange(from, to)
end
return word
end -- GetWord2()
|