ExpressionResult GetExpressionAtCursor()
{
// TextLocation memberLocation;
// if (currentMember != null) {
// memberLocation = currentMember.Region.Begin;
// } else if (currentType != null) {
// memberLocation = currentType.Region.Begin;
// } else {
// memberLocation = location;
// }
var baseUnit = ParseStub("a");
var tmpUnit = baseUnit;
AstNode expr = baseUnit.GetNodeAt(
location,
n => n is IdentifierExpression || n is MemberReferenceExpression
);
if (expr == null) {
expr = baseUnit.GetNodeAt<AstType>(location.Line, location.Column - 1);
}
if (expr == null)
expr = baseUnit.GetNodeAt<Identifier>(location.Line, location.Column - 1);
// try insertStatement
if (expr == null && baseUnit.GetNodeAt<EmptyStatement>(
location.Line,
location.Column
) != null) {
tmpUnit = baseUnit = ParseStub("a();", false);
expr = baseUnit.GetNodeAt<InvocationExpression>(
location.Line,
location.Column + 1
);
}
if (expr == null) {
baseUnit = ParseStub("()");
expr = baseUnit.GetNodeAt<IdentifierExpression>(
location.Line,
location.Column - 1
);
if (expr == null) {
expr = baseUnit.GetNodeAt<MemberType>(location.Line, location.Column - 1);
}
}
if (expr == null) {
baseUnit = ParseStub("a", false);
expr = baseUnit.GetNodeAt(
location,
n => n is IdentifierExpression || n is MemberReferenceExpression || n is CatchClause
);
}
// try statement
if (expr == null) {
expr = tmpUnit.GetNodeAt<SwitchStatement>(
location.Line,
location.Column - 1
);
baseUnit = tmpUnit;
}
if (expr == null) {
var block = tmpUnit.GetNodeAt<BlockStatement>(location);
var node = block != null ? block.Statements.LastOrDefault() : null;
var forStmt = node != null ? node.PrevSibling as ForStatement : null;
if (forStmt != null && forStmt.EmbeddedStatement.IsNull) {
expr = forStmt;
var id = new IdentifierExpression("stub");
forStmt.EmbeddedStatement = new BlockStatement() { Statements = { new ExpressionStatement (id) }};
expr = id;
baseUnit = tmpUnit;
}
}
if (expr == null) {
var forStmt = tmpUnit.GetNodeAt<ForeachStatement>(
location.Line,
location.Column - 3
);
if (forStmt != null && forStmt.EmbeddedStatement.IsNull) {
forStmt.VariableNameToken = Identifier.Create("stub");
expr = forStmt.VariableNameToken;
baseUnit = tmpUnit;
}
}
if (expr == null) {
expr = tmpUnit.GetNodeAt<VariableInitializer>(
location.Line,
location.Column - 1
);
baseUnit = tmpUnit;
}
// try parameter declaration type
if (expr == null) {
baseUnit = ParseStub(">", false, "{}");
expr = baseUnit.GetNodeAt<TypeParameterDeclaration>(
location.Line,
location.Column - 1
);
}
// try parameter declaration method
if (expr == null) {
baseUnit = ParseStub("> ()", false, "{}");
expr = baseUnit.GetNodeAt<TypeParameterDeclaration>(
location.Line,
location.Column - 1
);
}
// try expression in anonymous type "new { sample = x$" case
if (expr == null) {
baseUnit = ParseStub("a", false);
expr = baseUnit.GetNodeAt<AnonymousTypeCreateExpression>(
location.Line,
location.Column
);
if (expr != null) {
expr = baseUnit.GetNodeAt<Expression>(location.Line, location.Column) ?? expr;
}
if (expr == null) {
expr = baseUnit.GetNodeAt<AstType>(location.Line, location.Column);
}
}
if (expr == null) {
return null;
}
return new ExpressionResult(expr, baseUnit);
}