Exemple #1
0
        private static string GetIndentationStringFromBlockLevel(ParsedToken parsedToken, FormattingOptions formattingOptions)
        {
            int totalSpaces = GetTotalNumberOfSpaces(parsedToken.BlockLevel, formattingOptions);

            int spacesNeeded = totalSpaces;
            int tabsNeeded = 0;

            if (formattingOptions.UsingTabs && formattingOptions.TabSize > 0)
            {
                spacesNeeded = totalSpaces % (int)formattingOptions.TabSize;
                tabsNeeded = (totalSpaces - spacesNeeded) / (int)formattingOptions.TabSize;
            }

            return new string('\t', tabsNeeded) + new string(' ', spacesNeeded);
        }
Exemple #2
0
        internal static IEnumerable<TextEditInfo> GetIndentations(List<ParsedToken> parsedTokens, FormattingOptions formattingOptions)
        {
            Requires.NotNull(parsedTokens, nameof(parsedTokens));

            foreach (ParsedToken parsedToken in parsedTokens)
            {
                string indentationString =
                                  Indenter.GetIndentationStringFromBlockLevel(parsedToken, formattingOptions);

                foreach (IndentInfo indentInfo in Indenter.GetIndentInformation(parsedToken))
                {
                    yield return new TextEditInfo(new Range(indentInfo.Start, indentInfo.Length),
                        indentInfo.IsBeforeText ? indentationString : string.Empty);
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// This is main entry point for the VS side of things. For now, the implementation
        /// of the function is not final and it just used as a way seeing results in VS.
        /// Ideally, Format will also take in a "formatting option" object that dictates
        /// the rules that should be enabled, spacing and tabs.
        /// </summary>
        /// <param name="sourceText">The SourceText that represents the text to be formatted</param>
        /// <param name="range">The range of indicies to be formatted</param>
        /// <param name="formattingOptions">The options to format with, null leaves the options as they were</param>
        /// <returns>
        /// A list of TextEditInfo objects are returned for the spacing between tokens (starting from the
        /// first token in the document to the last token. After the spacing text edits, the indentation
        /// text edits follow (starting again from the beginning of the document). I might separate the
        /// indentation text edits from the spacing text edits in the future but for now they are in
        /// the same list.
        /// </returns>
        public List<TextEditInfo> Format(SourceText sourceText, Range range, FormattingOptions formattingOptions)
        {
            Requires.NotNull(formattingOptions, nameof(formattingOptions));
            Requires.NotNull(sourceText, nameof(sourceText));

            this.formattingOptions = formattingOptions;
            this.ruleMap = RuleMap.Create(this.formattingOptions.OptionalRuleMap);

            List<TextEditInfo> textEdits = new List<TextEditInfo>();

            SyntaxTree syntaxTree = this.parseTreeProvider.Get(sourceText);

            List<ParsedToken> parsedTokens = new List<ParsedToken>(ParsedToken.GetParsedTokens(syntaxTree, range));

            if (syntaxTree.ErrorList.Count == 0)
            {
                for (int i = 0; i < parsedTokens.Count - 1; ++i)
                {
                    FormattingContext formattingContext =
                        new FormattingContext(parsedTokens[i], parsedTokens[i + 1], sourceText);

                    Rule rule = this.ruleMap.Get(formattingContext);

                    if (rule != null)
                    {
                        textEdits.AddRange(rule.Apply(formattingContext));
                    }
                }
            }

            textEdits.AddRange(Indenter.GetIndentations(parsedTokens, this.formattingOptions));

            textEdits.Sort((x, y) => x.Start < y.Start ? 1 : x.Start == y.Start ? 0 : -1);

            return textEdits;
        }
Exemple #4
0
        private static string Format(string original, uint tabSize = 4, uint indentSize = 4, bool usingTabs = false, FormattingOptions formattingOptions = null)
        {
            LuaFeatureContainer featureContainer = new LuaFeatureContainer();
            Range range = new Range(0, original.Length);

            if (formattingOptions == null)
            {
                formattingOptions = new FormattingOptions(new List<DisableableRules>(), tabSize, indentSize, usingTabs);
            }

            List<TextEditInfo> textEdits = featureContainer.Formatter.Format(new SourceText(original), range, formattingOptions);

            var buffer = host.CreateTextBuffer(original);
            var edit = buffer.CreateEdit();

            foreach (var textEdit in textEdits)
            {
                edit.Replace(textEdit.Start, textEdit.Length, textEdit.ReplacingWith);
            }

            var applied = edit.Apply();

            return applied.GetText();
        }
Exemple #5
0
 private static int GetTotalNumberOfSpaces(int level, FormattingOptions globalOptions)
 {
     return level * (int)globalOptions.IndentSize;
 }
Exemple #6
0
 internal static int GetIndentationFromPosition(SyntaxTree syntaxTree, FormattingOptions formattingOptions, int position)
 {
     int level = GetIndentLevelFromPosition(syntaxTree, position);
     return GetTotalNumberOfSpaces(level, formattingOptions);
 }
        private FormattingOptions GetFormattingOptions(UserSettings settings)
        {

            List<DisableableRules> disabledRules = this.GetDisabledRules(settings);

            FormattingOptions formattingOptions = new FormattingOptions(disabledRules, settings.TabSize, settings.IndentSize, settings.UsingTabs);

            return formattingOptions;
        }
Exemple #8
0
 internal static void FormattingTest(string original, string expected, FormattingOptions formattingOptions)
 {
     string actual = Format(original, formattingOptions);
     Assert.Equal(expected, actual);
 }
Exemple #9
0
 private static string Format(string original, FormattingOptions formattingOptions)
 {
     return Format(original, 4, 4, false, formattingOptions);
 }