コード例 #1
0
ファイル: Indenter.cs プロジェクト: Strongc/VSLua
        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);
        }
コード例 #2
0
ファイル: Indenter.cs プロジェクト: Strongc/VSLua
        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);
                }
            }
        }
コード例 #3
0
ファイル: Formatter.cs プロジェクト: Strongc/VSLua
        /// <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;
        }
コード例 #4
0
ファイル: Tester.cs プロジェクト: Strongc/VSLua
        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();
        }
コード例 #5
0
ファイル: Indenter.cs プロジェクト: Strongc/VSLua
 private static int GetTotalNumberOfSpaces(int level, FormattingOptions globalOptions)
 {
     return level * (int)globalOptions.IndentSize;
 }
コード例 #6
0
ファイル: Indenter.cs プロジェクト: Strongc/VSLua
 internal static int GetIndentationFromPosition(SyntaxTree syntaxTree, FormattingOptions formattingOptions, int position)
 {
     int level = GetIndentLevelFromPosition(syntaxTree, position);
     return GetTotalNumberOfSpaces(level, formattingOptions);
 }
コード例 #7
0
        private FormattingOptions GetFormattingOptions(UserSettings settings)
        {

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

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

            return formattingOptions;
        }
コード例 #8
0
ファイル: Tester.cs プロジェクト: Microsoft/VSLua
 internal static void FormattingTest(string original, string expected, FormattingOptions formattingOptions)
 {
     string actual = Format(original, formattingOptions);
     Assert.Equal(expected, actual);
 }
コード例 #9
0
ファイル: Tester.cs プロジェクト: Microsoft/VSLua
 private static string Format(string original, FormattingOptions formattingOptions)
 {
     return Format(original, 4, 4, false, formattingOptions);
 }