public EditorConfigConfiguration(string formatConfigFile)
        {
            var parser = new EditorConfigParser(formatConfigFile);
            FileConfiguration configFile = parser.Parse(formatConfigFile).First();

            if (configFile.Properties.TryGetValue("enable", out string enableAsString) && bool.TryParse(enableAsString, out bool enableParsed))
            {
                enable = enableParsed;
            }

            if (configFile.Properties.ContainsKey("allowed_extensions"))
            {
                configFile.Properties.TryGetValue("allowed_extensions", out allowed);
            }

            if (configFile.Properties.ContainsKey("denied_extensions"))
            {
                configFile.Properties.TryGetValue("denied_extensions", out denied);
            }

            if (configFile.Properties.ContainsKey("command"))
            {
                configFile.Properties.TryGetValue("command", out command);
            }

            if (configFile.Properties.TryGetValue("enable_in_debug", out string enableInDebugAsString) && bool.TryParse(enableInDebugAsString, out bool enableInDebugParsed))
            {
                enableInDebug = enableInDebugParsed;
            }
        }
Пример #2
0
        void UpdatePolicy(Solution solution, string mimeType, string extension)
        {
            var configuration = _parser.Parse(
                Path.Combine(solution.BaseDirectory, "FileDoesntHaveToExist" + extension)).First();

            if (configuration == null || configuration.Properties.Count == 0)
            {
                return;
            }

            var mimeTypes = DesktopService.GetMimeTypeInheritanceChain(mimeType);
            var oldPolicy = solution.Policies.Get <TextStylePolicy>(mimeTypes);
            var fileWidth = configuration.MaxLineLength.HasValue ?
                            configuration.MaxLineLength.Value : oldPolicy.FileWidth;
            var eolMarkerVal = TranslateEndOfLine(configuration.EndOfLine);
            var eolMarker    = eolMarkerVal.HasValue ?
                               eolMarkerVal.Value : oldPolicy.EolMarker;
            var indentWidthVal = GetIndentWidth(configuration);
            var indentWidth    = indentWidthVal.HasValue ?
                                 indentWidthVal.Value : oldPolicy.IndentWidth;
            var tabWidth = configuration.TabWidth.HasValue ?
                           configuration.TabWidth.Value : oldPolicy.TabWidth;
            var tabsToSpaces = configuration.IndentStyle.HasValue ?
                               (configuration.IndentStyle.Value == IndentStyle.Space) : oldPolicy.TabsToSpaces;
            var removeTrailingWhitespace = configuration.TrimTrailingWhitespace.HasValue ?
                                           configuration.TrimTrailingWhitespace.Value : oldPolicy.RemoveTrailingWhitespace;
            var newPolicy = new TextStylePolicy(fileWidth, tabWidth, indentWidth, tabsToSpaces,
                                                oldPolicy.NoTabsAfterNonTabs, removeTrailingWhitespace, eolMarker);

            if (newPolicy != oldPolicy)
            {
                solution.Policies.Set <TextStylePolicy>(newPolicy, mimeType);
            }
        }
Пример #3
0
        protected FileConfiguration GetConfig(MethodBase method, string fileName, string configurationFile = ".editorconfig")
        {
            var file        = this.GetFileFromMethod(method, fileName);
            var parser      = new EditorConfigParser(configurationFile);
            var fileConfigs = parser.Parse(file);

            fileConfigs.Should().NotBeNull();
            return(fileConfigs);
        }
 public static bool TryLoad(string path, out FileConfiguration fileConfiguration)
 {
     fileConfiguration = null;
     var parser = new EditorConfigParser();
     var configurations = parser.Parse(path);
     if (!configurations.Any()) return false;
     fileConfiguration = configurations.First();
     return true;
 }
Пример #5
0
        public static FileConfiguration ParseConfig(Document doc)
        {
            if (doc == null)
            {
                return(null);
            }

            EditorConfigParser parser = new EditorConfigParser();
            IEnumerable <FileConfiguration> configs = parser.Parse(doc.Name);
            FileConfiguration config = configs.First();

            return(config);
        }
Пример #6
0
        public static bool TryLoad(string path, out FileConfiguration fileConfiguration)
        {
            fileConfiguration = null;
            var parser         = new EditorConfigParser();
            var configurations = parser.Parse(path);

            if (!configurations.Any())
            {
                return(false);
            }
            fileConfiguration = configurations.First();
            return(true);
        }
        protected static FileConfiguration GetConfig(MethodBase?method, string fileName, string configurationFile = ".editorconfig")
        {
            if (method == null)
            {
                throw new ArgumentException("The method must not be null", nameof(method));
            }

            var file        = GetFileFromMethod(method, fileName);
            var parser      = new EditorConfigParser(configurationFile);
            var fileConfigs = parser.Parse(file);

            fileConfigs.Should().NotBeNull();
            return(fileConfigs);
        }
Пример #8
0
        static void Main(string[] args)
        {
            try
            {
                var arguments = new ArgumentsParser(args);
                if (arguments.PrintVersion)
                {
                    Console.WriteLine(_fullVersionInfo);
                    return;
                }
                if (arguments.PrintHelp)
                {
                    Console.WriteLine(_usage.Trim());
                    return;
                }

                var configParser = new EditorConfigParser(arguments.ConfigFileName, arguments.DevelopVersion);

                var results = configParser.Parse(arguments.FileNames).ToList();
                if (!results.Any())
                {
                    PrintError("Did not find any config for files:{0}", string.Join(",", args));
                    Environment.Exit(1);
                }

                PrintParserResults(results.ToList());
            }
            catch (ApplicationArgumentException e)
            {
                PrintUsage(e.Message);
                Environment.Exit(1);
            }
            catch (Exception e)
            {
                PrintError(e.Message);
                Environment.Exit(1);
            }
        }
Пример #9
0
        static FileFormatting LoadEditorConfig(string filepath)
        {
            if (File.Exists(".editorconfig"))
            {
                EditorConfigParser  parser               = new EditorConfigParser();
                FileConfiguration[] configurations       = parser.Parse(filepath).ToArray();
                FileConfiguration   currentConfiguration = null;
                Encoding            encoding             = null;
                string intend     = null;
                string lineEnding = null;
                switch (configurations.Length)
                {
                case 0:
                    return(new FileFormatting(Environment.NewLine, "    ", Utils.GetEncoding(filepath)));

                case 1:
                    currentConfiguration = configurations[0];
                    break;

                default:
                    currentConfiguration = configurations[configurations.Length - 1];
                    break;
                }
                switch (currentConfiguration.Charset)
                {
                case null:
                    encoding = Utils.GetEncoding(filepath);
                    break;

                case Charset.Latin1:
                    encoding = Utils.Latin1Encoding;
                    break;

                case Charset.UTF8:
                    encoding = Utils.Utf8NoBomEncoding;
                    break;

                case Charset.UTF16BE:
                    encoding = Utils.Utf16BeBomEncoding;
                    break;

                case Charset.UTF16LE:
                    encoding = Utils.Utf16LeBomEncoding;
                    break;

                case Charset.UTF8BOM:
                    encoding = Utils.Utf8BomEncoding;
                    break;
                }

                switch (currentConfiguration.EndOfLine)
                {
                case null:
                    lineEnding = Environment.NewLine;
                    break;

                case EndOfLine.LF:
                    lineEnding = "\n";
                    break;

                case EndOfLine.CRLF:
                    lineEnding = "\r\n";
                    break;

                case EndOfLine.CR:
                    lineEnding = "\r";
                    break;
                }

                switch (currentConfiguration.IndentStyle)
                {
                case null:
                    intend = "    ";
                    break;

                case IndentStyle.Tab:
                    intend = "	";
                    break;

                case IndentStyle.Space:
                    int?count = currentConfiguration.IndentSize.NumberOfColumns;
                    if (count != null)
                    {
                        int           spacecount = count.Value;
                        StringBuilder sb         = new StringBuilder(spacecount, spacecount);
                        for (int i = 0; i < spacecount; i++)
                        {
                            sb.Append(' ');
                        }

                        intend = sb.ToString();
                    }
                    else
                    {
                        intend = "    ";
                    }
                    break;
                }
                return(new FileFormatting(lineEnding, intend, encoding));
            }

            return(new FileFormatting(Environment.NewLine, "    ", Utils.GetEncoding(filepath)));
        }
Пример #10
0
        internal static SourceCodeEditorSettings ReadSettings(ITextBuffer buffer, string fileName)
        {
            var configuration = configParser.Parse(fileName);
            var settings      = new SourceCodeEditorSettings(); // this gets a copy of the current Tools/Options settings

            if (configuration.Properties.Count > 0)
            {
                if (configuration.IndentStyle.HasValue)
                {
                    settings.TabsAsSpaces = configuration.IndentStyle.Value == IndentStyle.Space;
                }
                if (configuration.TabWidth.HasValue)
                {
                    settings.TabSize = configuration.TabWidth.Value;
                }
                if (configuration.IndentSize != null)
                {
                    if (configuration.IndentSize.NumberOfColumns.HasValue)
                    {
                        settings.IndentSize = configuration.IndentSize.NumberOfColumns.Value;
                    }
                    else if (configuration.IndentSize.UseTabWidth)
                    {
                        settings.IndentSize = settings.TabSize;
                    }
                }
                if (configuration.TrimTrailingWhitespace.HasValue)
                {
                    settings.TrimTrailingWhiteSpace = configuration.TrimTrailingWhitespace.Value;
                }
                if (configuration.InsertFinalNewline.HasValue)
                {
                    settings.InsertFinalNewline = configuration.InsertFinalNewline.Value;
                }

                if (configuration.Properties.ContainsKey(KEYWORDCASE))
                {
                    var temp = configuration.Properties[KEYWORDCASE].ToLower();
                    switch (temp)
                    {
                    case UPPER:
                        settings.KeywordCase = KeywordCase.Upper;
                        break;

                    case LOWER:
                        settings.KeywordCase = KeywordCase.Lower;
                        break;

                    case TITLE:
                        settings.KeywordCase = KeywordCase.Title;
                        break;

                    default:
                        settings.KeywordCase = KeywordCase.None;
                        break;
                    }
                }
                if (configuration.Properties.ContainsKey(IDENTIFIERCASE))
                {
                    var temp = configuration.Properties[IDENTIFIERCASE].ToLower();
                    settings.IdentifierCase = temp == TRUE;
                }
                if (configuration.Properties.ContainsKey(UDCCASE))
                {
                    var temp = configuration.Properties[UDCCASE].ToLower();
                    settings.UDCKeywordCase = temp == TRUE;
                }
            }
            if (buffer.Properties.ContainsProperty(typeof(SourceCodeEditorSettings)))
            {
                buffer.Properties.RemoveProperty(typeof(SourceCodeEditorSettings));
            }
            buffer.Properties.AddProperty(typeof(SourceCodeEditorSettings), settings);
            return(settings);
        }
 /// <summary>
 /// Parses a <see cref="SourceText"/> and returns all discovered naming style options and their locations
 /// </summary>
 /// <param name="editorConfigText">The <see cref="SourceText"/> contents of the editorconfig file.</param>
 /// <param name="pathToEditorConfigFile">The full path to the editorconfig file on disk.</param>
 /// <returns>A type that represents all discovered naming style options in the given <see cref="SourceText"/>.</returns>
 public static EditorConfigNamingStyles Parse(SourceText editorConfigText, string?pathToEditorConfigFile = null)
 => EditorConfigParser.Parse <EditorConfigNamingStyles, NamingStyleOption, NamingStyleOptionAccumulator>(editorConfigText, pathToEditorConfigFile, new NamingStyleOptionAccumulator());
Пример #12
0
    // Called by the core when a file matching Mask is opened in the editor.
    public override void Invoke(IEditor editor, ModuleEditorEventArgs e)
    {
        // get the file name, it may be amended
        var fileName = editor.FileName;

        // ?NewFile?
        if (fileName.Contains("?"))
        {
            return;
        }

        // get the usual configurations
        var parser         = new EditorConfigParser();
        var configurations = parser.GetConfigurationFilesTillRoot(fileName);

        if (configurations.Count == 0)
        {
            // get the profile configurations to use with the amended file
            var defaults = GetProfileConfigurations(ref fileName);
            if (defaults == null)
            {
                return;
            }

            configurations = defaults;
        }

        // get the properties for the original or amended file
        var config     = parser.Parse(fileName, configurations);
        var properties = config.Properties;

        if (properties.TryGetValue(key_trim_trailing_whitespace, out var trim) && string.Equals(trim, "true", StringComparison.OrdinalIgnoreCase))
        {
            do_trim_trailing_whitespace = true;
        }

        if (properties.TryGetValue(key_insert_final_newline, out var eof) && string.Equals(eof, "true", StringComparison.OrdinalIgnoreCase))
        {
            do_insert_final_newline = true;
        }

        if (do_trim_trailing_whitespace || do_insert_final_newline)
        {
            editor.Saving += OnSaving;
        }

        if (properties.TryGetValue(key_indent_style, out var style))
        {
            switch (style)
            {
            case "tab":
                editor.ExpandTabs = ExpandTabsMode.None;
                break;

            case "space":
                editor.ExpandTabs = editor.DisableHistory ? ExpandTabsMode.New : ExpandTabsMode.All;
                break;
            }
        }

        if (properties.TryGetValue(key_indent_size, out var size))
        {
            if (int.TryParse(size, out var value) && value > 0)
            {
                editor.TabSize = value;
            }
        }

        if (properties.TryGetValue(key_charset, out var charset))
        {
            var codePage = editor.CodePage;
            switch (charset.ToLower())
            {
            case "utf-8":
                if (editor.CodePage != 65001)
                {
                    editor.CodePage = 65001;
                }
                if (editor.WriteByteOrderMark)
                {
                    editor.WriteByteOrderMark = false;
                }
                break;

            case "utf-8-bom":
                if (editor.CodePage != 65001)
                {
                    editor.CodePage = 65001;
                }
                if (!editor.WriteByteOrderMark)
                {
                    editor.WriteByteOrderMark = true;
                }
                break;

            case "utf-16le":
                if (editor.CodePage != 1200)
                {
                    editor.CodePage = 1200;
                }
                editor.WriteByteOrderMark = true;
                break;

            case "utf-16be":
                if (editor.CodePage != 1201)
                {
                    editor.CodePage = 1201;
                }
                editor.WriteByteOrderMark = true;
                break;
            }
        }
    }