protected override bool IsPartialMethodCompletionContext(SyntaxTree tree, int position, CancellationToken cancellationToken, out DeclarationModifiers modifiers, out SyntaxToken token) { var touchingToken = tree.FindTokenOnLeftOfPosition(position, cancellationToken); var targetToken = touchingToken.GetPreviousTokenIfTouchingWord(position); var text = tree.GetText(cancellationToken); token = targetToken; modifiers = default(DeclarationModifiers); if (targetToken.IsKind(SyntaxKind.VoidKeyword, SyntaxKind.PartialKeyword) || (targetToken.Kind() == SyntaxKind.IdentifierToken && targetToken.HasMatchingText(SyntaxKind.PartialKeyword))) { return !IsOnSameLine(touchingToken.GetNextToken(), touchingToken, text) && VerifyModifiers(tree, position, cancellationToken, out modifiers); } return false; }
static HashSet<string> GetInitializedMembers (SyntaxTree tree, int position, CancellationToken cancellationToken) { var token = tree.FindTokenOnLeftOfPosition (position, cancellationToken) .GetPreviousTokenIfTouchingWord (position); // We should have gotten back a { or , if (token.Kind () == SyntaxKind.CommaToken || token.Kind () == SyntaxKind.OpenBraceToken) { if (token.Parent != null) { var initializer = token.Parent as InitializerExpressionSyntax; if (initializer != null) { return new HashSet<string> (initializer.Expressions.OfType<AssignmentExpressionSyntax> () .Where (b => b.OperatorToken.Kind () == SyntaxKind.EqualsToken) .Select (b => b.Left) .OfType<IdentifierNameSyntax> () .Select (i => i.Identifier.ValueText)); } } } return new HashSet<string> (); }
internal static SyntaxNode GetObjectCreationNewExpression (SyntaxTree tree, int position, CancellationToken cancellationToken) { if (tree != null) { if (!tree.IsInNonUserCode (position, cancellationToken)) { var tokenOnLeftOfPosition = tree.FindTokenOnLeftOfPosition (position, cancellationToken); var newToken = tokenOnLeftOfPosition.GetPreviousTokenIfTouchingWord (position); // Only after 'new'. if (newToken.Kind () == SyntaxKind.NewKeyword) { // Only if the 'new' belongs to an object creation expression (and isn't a 'new' // modifier on a member). if (tree.IsObjectCreationTypeContext (position, tokenOnLeftOfPosition, cancellationToken)) { return newToken.Parent as ExpressionSyntax; } } } } return null; }
static bool VerifyModifiers(SyntaxTree tree, int position, CancellationToken cancellationToken/*, out DeclarationModifiers modifiers*/) { var touchingToken = tree.FindTokenOnLeftOfPosition(position, cancellationToken); var token = touchingToken.GetPreviousToken(); bool foundPartial = touchingToken.IsKindOrHasMatchingText(SyntaxKind.PartialKeyword); bool foundAsync = false; while (IsOnSameLine(token, touchingToken, tree.GetText(cancellationToken))) { if (token.IsKind(SyntaxKind.ExternKeyword, SyntaxKind.PublicKeyword, SyntaxKind.ProtectedKeyword, SyntaxKind.InternalKeyword)) { //modifiers = default(DeclarationModifiers); return false; } if (token.IsKindOrHasMatchingText(SyntaxKind.AsyncKeyword)) { foundAsync = true; } foundPartial = foundPartial || token.IsKindOrHasMatchingText(SyntaxKind.PartialKeyword); token = token.GetPreviousToken(); } /*modifiers = new DeclarationModifiers() .WithPartial(true) .WithAsync (foundAsync);*/ return foundPartial; }
protected override SyntaxToken GetToken(CompletionItem completionItem, SyntaxTree tree, CancellationToken cancellationToken) { var tokenSpanEnd = MemberInsertionCompletionItem.GetTokenSpanEnd(completionItem); return tree.FindTokenOnLeftOfPosition(tokenSpanEnd, cancellationToken); }