Commits

SirAlaran committed b45494a

keyword completion. Snippet spacing fixes

Comments (0)

Files changed (1)

 "                "};
 ]]
 
+M.KEYWORD = [[
+/* XPM */
+static char * keyword_xpm[] = {
+"16 16 2 1",
+" 	c None",
+"+	c #000000",
+"                ",
+"  ++     +++    ",
+"  ++    +++     ",
+"  ++   +++      ",
+"  ++  +++       ",
+"  ++ +++        ",
+"  +++++         ",
+"  ++++          ",
+"  +++++         ",
+"  ++ +++        ",
+"  ++  +++       ",
+"  ++   +++      ",
+"  ++    +++     ",
+"  ++     +++    ",
+"  ++      +++   ",
+"                "};
+]]
+
 
 -- For this module to work the dscanner program must be installed. Configure the
 -- path to the executable here
 	filename = 1, line = 2, message = 3
 }
 
+local keywords = {
+	"abstract?5", "align?5", "asm?5", "assert?5", "auto?5", "body?5", "break?5", "case?5", "cast?5",
+	"catch?5", "const?5", "continue?5", "debug?5", "default?5", "delete?5",
+	"deprecated?5", "do?5", "else?5", "extern?5", "export?5", "false?5", "final?5", "finally?5",
+	"for?5", "foreach?5", "foreach_reverse?5", "goto?5", "if?5", "import?5", "immutable?5",
+	"in?5", "inout?5", "invariant?5", "is?5", "lazy?5", "macro?5", "mixin?5", "new?5", "nothrow?5",
+	"null?5", "out?5", "override?5", "pragma?5", "private?5", "protected?5", "public?5", "pure?5",
+	"ref?5", "return?5", "scope?5", "shared?5", "static?5", "super?5", "switch?5",
+	"synchronized?5", "this?5", "throw?5","true?5", "try?5", "typeid?5", "typeof?5", "unittest?5",
+	"version?5", "volatile?5", "while?5", "with?5", "__gshared?5", "__thread?5", "__traits?5"
+}
+
 -- All types have these fields
 local commonFields = {
 	["alignof"] = "size_t", ["init"] = "<<self>>", ["mangleof"] = "string",
 	completionInfo.overloads = {}
 	completionInfo.positions = {}
 	completionInfo.files = {}
-	completionInfo.currentCalltip = 1
-	completionInfo.callTips = {}
 	completionInfo.currentOverloads = {}
 	completionInfo.importDirectories = {}
 end
 			for _, pattern in ipairs(declarations) do
 				local typeName = line:match(pattern..symbol.."%f[%s%p_]")
 				if typeName == "auto" then
-					local autoPattern = "auto%s+"..symbol.."%s*=%s*(%w+)"
-					print(autoPattern)
+					local autoPattern = "auto%s+"..symbol.."%s*=%s*([^%s;]+)"
 					local typeName = line:match(autoPattern)
 					if typeName == "new" then
-						autoPattern = "auto%s+"..symbol.."%s*=%s*new%s+(%w+)"
+						autoPattern = "auto%s+"..symbol.."%s*=%s*new%s+([^%s;]+)"
 						typeName = line:match(autoPattern)
+					else
+						if typeName:match("^\"") then return "string", lineNumber end
+						if typeName:match("^%[") then return "array", lineNumber end
+						if typeName:match("^%d+[L]?$") then return "int", lineNumber end
+						if typeName:match("^%d+%.%d+[f]?$") then return "double", lineNumber end
 					end
 					return typeName, lineNumber
 				elseif typeName and typeName ~= "return" then
 	for builtinType, properties in pairs(builtinTypes) do
 		completionInfo.symbols[builtinType] = {variables = {}, functions = {}}
 		for property, propertyType in pairs(properties) do
-			table.insert(completionInfo.symbols[builtinType].variables, property)
+			table.insert(completionInfo.symbols[builtinType].variables, property .. "?1")
 			completionInfo.types[builtinType .. "." .. property] = propertyType
 		end
 	end
 	return completions
 end
 
-local function displayCompletionList(completions, len)
+local function registerImages()
 	buffer:register_image(1, M.FIELDS)
 	buffer:register_image(2, M.FUNCTIONS)
 	buffer:register_image(3, M.PACKAGE)
 	buffer:register_image(4, M.MODULE)
+	buffer:register_image(5, M.KEYWORD)
+end
+
+local function displayCompletionList(completions, len)
+	registerImages()
 	local setting = buffer.auto_c_choose_single
 	buffer.auto_c_choose_single = false
 	buffer:auto_c_show(len, table.concat(completions, " "))
 -- Allow arrow key presses to cycle between call tips
 events.connect(events.CALL_TIP_CLICK, function(arrow)
 	if buffer:get_lexer() ~= "dmd" then return end
-	if arrow == 1 and completionInfo.currentCalltip > 1 then
+	if arrow == 1 then
 		completionInfo.currentCalltip = completionInfo.currentCalltip - 1
-	elseif arrow == 2 and completionInfo.currentCalltip < #completionInfo.callTips then
+	elseif arrow == 2 then
 		completionInfo.currentCalltip = completionInfo.currentCalltip + 1
 	end
+	if completionInfo.currentCalltip > #completionInfo.callTips then
+		completionInfo.currentCalltip = #completionInfo.callTips
+	elseif completionInfo.currentCalltip <= 1 then
+		completionInfo.currentCalltip = 1
+	end
+	print(completionInfo.currentCalltip, #completionInfo.callTips)
 	buffer:call_tip_show(buffer:call_tip_pos_start(),
 		completionInfo.callTips[completionInfo.currentCalltip])
 end)
 	end
 end
 
+local function filterCompletions(completions, part)
+	local filteredCompletions = {}
+	for _, completion in ipairs(completions) do
+		if (buffer.auto_c_ignore_case and completion:lower():match("^"..part:lower()))
+				or completion:match("^"..part) then
+			filteredCompletions[#filteredCompletions + 1] = completion
+		end
+	end
+	return filteredCompletions
+end
+
 -- Called when the keyboard shortcut for autocomplete is pressed
 local function showAutocomplete()
-
 	if buffer:get_cur_line():match("^%s*import[^%w]+") then
 		showImports()
 		return
 
 	-- Do not do code completion inside of comments or strings
 	if style == 3 or style == 2 then
-		_M.textadept.editing.autocomplete_word("%w_")
+		_M.textadept.editing.autocomplete_word("%w_", keywords)
 		return
 	end
 	local symbol = getSymbol(position)
 	if symbol == nil then
-		_M.textadept.editing.autocomplete_word("%w_")
+		_M.textadept.editing.autocomplete_word("%w_", keywords)
 		return
 	end
 	local before = symbol:match("^([^%.]+)")
 		return
 	end
 	local members = completionInfo.symbols[container]
-	if members == nil then return end
+	if members == nil then
+		_M.textadept.editing.autocomplete_word("%w_", keywords)
+		return
+	end
 	local completions = getCompletions(members)
-	if not completions or #completions == 0 then
-		_M.textadept.editing.autocomplete_word("%w_")
-	return end
-	sortCompletions(completions)
-	displayCompletionList(completions, #part)
+	if container == "" or container == nil then
+		for _, keyword in ipairs(keywords) do
+			completions[#completions + 1] = keyword
+		end
+		sortCompletions(completions)
+		completions = filterCompletions(completions, part)
+		registerImages()
+		_M.textadept.editing.autocomplete_word("%w_", completions)
+	else
+		displayCompletionList(completions, #part)
+	end
 end
 
 local function completeVersion()
 {
 	%0
 }]],
-		['for'] = [[for(%1(initilization); %2(condition); %3(increment))
+		['for'] = [[for (%1(initilization); %2(condition); %3(increment))
 {
 	%0
 }]],
-		fore = [[foreach(%1(var); %2(range))
+		fore = [[foreach (%1(var); %2(range))
 {
 	%0
 }]],
-		forei = [[foreach(%1(i); 0..%2(n))
+		forei = [[foreach (%1(i); 0..%2(n))
 {
 	%0
 }]],
-		forr = [[foreach(ref %1(var); %2(range))
+		forr = [[foreach (ref %1(var); %2(range))
 {
 	%0
 }]],
-		fori = [[for(size_t i = 0; i != %1(condition); ++i)
+		fori = [[for (size_t i = 0; i != %1(condition); ++i)
 {
 	%0
 }]],
-		['while'] = [[while(%1(condition))
+		['while'] = [[while (%1(condition))
 {
 	%0
 }]],
-		['if'] = [[if(%1(condition))
+		['if'] = [[if (%1(condition))
 {
 	%0
 }]],
 		dw = [[do
 {
 	%0
-} while(%1(condition));]],
-		switch = [[switch(%1(value))
+} while (%1(condition));]],
+		switch = [[switch (%1(value))
 {
-	case %2:
-		%0
-		break;
-	default:
-		break;
+%0
+default:
+	break;
+}]],
+		fswitch = [[final switch (%1(value))
+{
+%0
+default:
+	break;
 }]],
 		case = [[case %1:
 	%0