Slightly better autocomplete for arrays. It still doesn't work properly
This commit is contained in:
parent
22a62bfe84
commit
0d23da9ea6
|
@ -99,6 +99,8 @@ body
|
|||
case TokenType.Delete:
|
||||
case TokenType.LBrace:
|
||||
case TokenType.LParen:
|
||||
case TokenType.Equals:
|
||||
case TokenType.Plus:
|
||||
case TokenType.Import:
|
||||
case TokenType.LBracket:
|
||||
case TokenType.Comma:
|
||||
|
@ -156,6 +158,7 @@ struct AutoComplete
|
|||
|
||||
string getTypeOfExpression(const(Token)[] expression, const Token[] tokens, size_t cursor)
|
||||
{
|
||||
stderr.writeln("getting type of ", expression);
|
||||
if (expression.length == 0)
|
||||
return "void";
|
||||
auto type = typeOfVariable(expression[0], cursor);
|
||||
|
@ -182,25 +185,9 @@ struct AutoComplete
|
|||
if (symbol.value in typeProperties)
|
||||
return symbol.value;
|
||||
|
||||
switch (symbol.type)
|
||||
{
|
||||
case TokenType.FloatLiteral:
|
||||
return "float";
|
||||
case TokenType.DoubleLiteral:
|
||||
return "double";
|
||||
case TokenType.RealLiteral:
|
||||
return "real";
|
||||
case TokenType.IntLiteral:
|
||||
return "int";
|
||||
case TokenType.UnsignedIntLiteral:
|
||||
return "uint";
|
||||
case TokenType.LongLiteral:
|
||||
return "long";
|
||||
case TokenType.UnsignedLongLiteral:
|
||||
return "ulong";
|
||||
default:
|
||||
break;
|
||||
}
|
||||
string tokenType = getTypeFromToken(symbol);
|
||||
if (tokenType !is null)
|
||||
return tokenType;
|
||||
|
||||
if (context.getMembersOfType(symbol.value))
|
||||
return symbol.value;
|
||||
|
@ -220,18 +207,28 @@ struct AutoComplete
|
|||
{
|
||||
// Found the symbol, now determine if it was declared here.
|
||||
auto p = preceedingTokens[index - 1];
|
||||
|
||||
|
||||
if ((p == TokenType.Auto || p == TokenType.Immutable
|
||||
|| p == TokenType.Const)
|
||||
&& preceedingTokens[index + 1] == TokenType.Assign)
|
||||
{
|
||||
return null;
|
||||
// Try to determine the type of a variable declared as "auto"
|
||||
return getTypeOfExpression(
|
||||
tokens[index + 2 .. findEndOfExpression(tokens, index + 2)],
|
||||
tokens, cursor);
|
||||
}
|
||||
else if (p == TokenType.Identifier
|
||||
|| (p.type > TokenType.TYPES_BEGIN
|
||||
&& p.type < TokenType.TYPES_END))
|
||||
{
|
||||
// Handle simple cases like "int a;" or "Someclass instance;"
|
||||
return p.value;
|
||||
}
|
||||
else if (p == TokenType.RBracket || p == TokenType.RParen)
|
||||
{
|
||||
return combineTokens(tokens[findBeginningOfExpression(tokens, index) .. index]);
|
||||
}
|
||||
}
|
||||
if (index == 0)
|
||||
break;
|
||||
|
@ -307,6 +304,8 @@ struct AutoComplete
|
|||
auto expressionType = getTypeOfExpression(
|
||||
splitCallChain(tokens[startIndex .. index]), tokens, cursor);
|
||||
|
||||
stderr.writeln("expression type is ", expressionType);
|
||||
|
||||
// Complete pointers and references the same way
|
||||
if (expressionType[$ - 1] == '*')
|
||||
expressionType = expressionType[0 .. $ - 1];
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
module langutils;
|
||||
import std.array;
|
||||
|
||||
|
||||
/**
|
||||
* Returns: true if input is a access attribute
|
||||
|
@ -43,6 +45,13 @@ pure nothrow TokenType lookupTokenType(const string input)
|
|||
return TokenType.Identifier;
|
||||
}
|
||||
|
||||
string combineTokens(ref const Token[] tokens)
|
||||
{
|
||||
auto app = appender!string();
|
||||
foreach (t; tokens)
|
||||
app.put(t.value);
|
||||
return app.data;
|
||||
}
|
||||
|
||||
pure nothrow TokenType lookupTokenTypeOptimized(const string input)
|
||||
{
|
||||
|
|
84
types.d
84
types.d
|
@ -602,37 +602,60 @@ public:
|
|||
}
|
||||
|
||||
|
||||
immutable(string[][string]) typeProperties;
|
||||
immutable(string[]) floatProperties;
|
||||
immutable(string[]) integralProperties;
|
||||
immutable(string[]) commonProperties;
|
||||
immutable(string[]) arrayProperties;
|
||||
immutable(string[string][string]) typeProperties; // Yo dawg I heard you like maps...
|
||||
immutable(string[string]) floatProperties;
|
||||
immutable(string[string]) integralProperties;
|
||||
immutable(string[string]) commonProperties;
|
||||
immutable(string[string]) arrayProperties;
|
||||
|
||||
static this()
|
||||
{
|
||||
floatProperties = ["alignof", "dig", "epsilon", "im", "infinity", "init",
|
||||
"mangleof", "mant_dig", "max", "max_10_exp", ".max_exp", "min_10_exp",
|
||||
"min_exp", "min_normal", "nan", "re", "sizeof"
|
||||
// <#> means "its own type"
|
||||
// for example float.max is of type float
|
||||
floatProperties = [
|
||||
"alignof" : "int",
|
||||
"dig" : "<#>",
|
||||
"epsilon" : "<#>",
|
||||
"im" : "<#>",
|
||||
"infinity" : "<#>",
|
||||
"init" : "<#>",
|
||||
"mangleof" : "string",
|
||||
"mant_dig" : "int",
|
||||
"max" : "<#>",
|
||||
"max_10_exp" : "int",
|
||||
"max_exp" : "int",
|
||||
"min_10_exp" : "int",
|
||||
"min_exp" : "int",
|
||||
"min_normal" : "<#>",
|
||||
"nan" : "<#>",
|
||||
"re" : "<#>",
|
||||
"sizeof" : "size_t"
|
||||
];
|
||||
|
||||
integralProperties = ["alignof", "init", "mangleof", "max",
|
||||
"min", "sizeof", "stringof"
|
||||
integralProperties = [
|
||||
"alignof" : "int",
|
||||
"init" : "<#>",
|
||||
"mangleof" : "string",
|
||||
"max" : "<#>",
|
||||
"min" : "<#>",
|
||||
"sizeof" : "size_t",
|
||||
"stringof" : "string"
|
||||
];
|
||||
|
||||
commonProperties = [
|
||||
"alignof",
|
||||
"init",
|
||||
"mangleof",
|
||||
"stringof"
|
||||
"alignof" : "int",
|
||||
"init" : "<#>",
|
||||
"mangleof" : "string",
|
||||
"stringof" : "string"
|
||||
];
|
||||
|
||||
arrayProperties = [
|
||||
"alignof",
|
||||
"init",
|
||||
"length",
|
||||
"mangleof",
|
||||
"ptr",
|
||||
"stringof",
|
||||
"alignof" : "int",
|
||||
"init" : "<#>",
|
||||
"length" : "size_t",
|
||||
"mangleof" : "string",
|
||||
"ptr" : "<#>*",
|
||||
"stringof" : "string",
|
||||
];
|
||||
|
||||
typeProperties = [
|
||||
|
@ -678,6 +701,26 @@ public:
|
|||
|
||||
Tuple!(string, string)[string] getMembersOfType(string name)
|
||||
{
|
||||
// Arrays
|
||||
if (name.length > 2 && name[$ - 2 .. $] == "[]")
|
||||
{
|
||||
Tuple!(string, string)[string] typeMap;
|
||||
foreach(k, v; arrayProperties)
|
||||
typeMap[k] = Tuple!(string, string)(v, "m");
|
||||
return typeMap;
|
||||
}
|
||||
|
||||
// Basic types
|
||||
auto tp = name in typeProperties;
|
||||
if (tp !is null)
|
||||
{
|
||||
Tuple!(string, string)[string] typeMap;
|
||||
foreach (k, v; *tp)
|
||||
typeMap[k] = Tuple!(string, string)(v.replace("<#>", name), "m");
|
||||
return typeMap;
|
||||
}
|
||||
|
||||
// User-defined types
|
||||
foreach (m; chain(modules, [currentModule]))
|
||||
{
|
||||
foreach (inherits; chain(m.interfaces, m.classes))
|
||||
|
@ -696,6 +739,7 @@ public:
|
|||
typeMap[k] = v;
|
||||
}
|
||||
}
|
||||
typeMap["classInfo"] = Tuple!(string, string)("TypeInfo_Class", "m");
|
||||
return typeMap;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue