Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indentation on colon not working for switches with patterns. #9097

Closed
CyrusNajmabadi opened this issue Feb 23, 2016 · 3 comments
Closed

Indentation on colon not working for switches with patterns. #9097

CyrusNajmabadi opened this issue Feb 23, 2016 · 3 comments
Assignees
Labels
Area-IDE Bug New Language Feature - Pattern Matching Pattern Matching Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented

Comments

@CyrusNajmabadi
Copy link
Member

In a normal switch, when you type colon in a case clause, the case clause is properly dedented and aligned. This does not work for case clauses with complex patterns in them.

@gafter
Copy link
Member

gafter commented Mar 3, 2016

@CyrusNajmabadi Can you please give me a hint where to start working on this?

@gafter gafter added the Blocked label Mar 3, 2016
@CyrusNajmabadi
Copy link
Member Author

I did a search for SyntaxKind.ColonToken and found these interesting locations:

        private static bool TokenShouldNotFormatOnTypeChar(SyntaxToken token)
        {
            return (token.IsKind(SyntaxKind.CloseParenToken) && !token.Parent.IsKind(SyntaxKind.UsingStatement)) ||
                (token.IsKind(SyntaxKind.ColonToken) && !(token.Parent.IsKind(SyntaxKind.LabeledStatement) || token.Parent.IsKind(SyntaxKind.CaseSwitchLabel) || token.Parent.IsKind(SyntaxKind.DefaultSwitchLabel)));
        }
...
                    case SyntaxKind.ColonToken:
                        {
                            var nonTerminalNode = token.Parent;
                            Contract.ThrowIfNull(nonTerminalNode, @"Malformed code or bug in parser???");

                            if (nonTerminalNode is SwitchLabelSyntax)
                            {
                                return GetIndentationOfLine(snapshot.GetLineFromPosition(nonTerminalNode.GetFirstToken(includeZeroWidth: true).SpanStart), OptionSet.GetOption(FormattingOptions.IndentationSize, token.Language));
                            }

                            // default case
                            return GetDefaultIndentationFromToken(token);
                        }
...
                case SyntaxKind.ColonToken:
                    if (previousToken.Parent is LabeledStatementSyntax || previousToken.Parent is SwitchLabelSyntax)
                    {
                        return 1;
                    }
...
            // case * :
            // default:
            // <label> :
            if (currentToken.IsKind(SyntaxKind.ColonToken))
            {
                if (currentToken.Parent.IsKind(SyntaxKind.CaseSwitchLabel,
                                               SyntaxKind.DefaultSwitchLabel,
                                               SyntaxKind.LabeledStatement,
                                               SyntaxKind.AttributeTargetSpecifier,
                                               SyntaxKind.NameColon))
                {
                    return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine);
                }
            }
...
                case SyntaxKind.ColonToken:
                    {
                        return FindAppropriateRangeForColon(endToken);
                    }
...
        public static bool IsColonInSwitchLabel(SyntaxToken token)
        {
            var switchLabel = token.Parent as SwitchLabelSyntax;
            return token.Kind() == SyntaxKind.ColonToken &&
                switchLabel != null &&
                switchLabel.ColonToken == token;
        }

I would personally start with some of these :)

gafter added a commit to gafter/roslyn that referenced this issue Mar 3, 2016
@gafter gafter added 4 - In Review A fix for the issue is submitted for review. Area-IDE Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented and removed 2 - Ready Blocked Area-Compilers 4 - In Review A fix for the issue is submitted for review. labels Mar 3, 2016
@gafter
Copy link
Member

gafter commented Mar 3, 2016

Fixed in dotnet:features/patterns via #9443.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Bug New Language Feature - Pattern Matching Pattern Matching Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Projects
None yet
Development

No branches or pull requests

4 participants