public IndentationResult GetDesiredIndentation(FormattingOptions.IndentStyle indentStyle) { // If the caller wants no indent, then we'll return an effective '0' indent. if (indentStyle == FormattingOptions.IndentStyle.None) { return(new IndentationResult(basePosition: 0, offset: 0)); } // find previous line that is not blank. this will skip over things like preprocessor // regions and inactive code. var previousLineOpt = GetPreviousNonBlankOrPreprocessorLine(); // it is beginning of the file, there is no previous line exists. // in that case, indentation 0 is our base indentation. if (previousLineOpt == null) { return(IndentFromStartOfLine(0)); } var previousNonWhitespaceOrPreprocessorLine = previousLineOpt.Value; // If the user wants block indentation, then we just return the indentation // of the last piece of real code. // // TODO(cyrusn): It's not clear to me that this is correct. Block indentation // should probably follow the indentation of hte last non-blank line *regardless // if it is inactive/preprocessor region. By skipping over thse, we are essentially // being 'smart', and that seems to be overriding the user desire to have Block // indentation. if (indentStyle == FormattingOptions.IndentStyle.Block) { // If it's block indentation, then just base return(GetIndentationOfLine(previousNonWhitespaceOrPreprocessorLine)); } Debug.Assert(indentStyle == FormattingOptions.IndentStyle.Smart); // Because we know that previousLine is not-whitespace, we know that we should be // able to get the last non-whitespace position. var lastNonWhitespacePosition = previousNonWhitespaceOrPreprocessorLine.GetLastNonWhitespacePosition().Value; var token = Root.FindToken(lastNonWhitespacePosition); Debug.Assert(token.RawKind != 0, "FindToken should always return a valid token"); return(_service.GetDesiredIndentationWorker( this, token, previousNonWhitespaceOrPreprocessorLine, lastNonWhitespacePosition)); }
private readonly IndentationResult?GetDesiredSmartIndentation() { // For smart indent, we generally will be computing from either the previous token in the code, or in a // few special cases, the previous trivia. var token = TryGetPrecedingVisibleToken(); // Look to see if we're immediately following some visible piece of trivia. There may // be some cases where we'll base our indent off of that. However, we only do this as // long as we're immediately after the trivia. If there are any blank lines between us // then we consider that unimportant for indentation. var trivia = TryGetImmediatelyPrecedingVisibleTrivia(); if (token == null && trivia == null) { return(null); } return(_service.GetDesiredIndentationWorker(this, token, trivia)); }