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