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;
            }
        }
        public static IUnitTestGeneratorOptions Create(string solutionFilePath, IGenerationOptions generationOptions, IVersioningOptions versioningOptions)
        {
            if (generationOptions == null)
            {
                throw new ArgumentNullException(nameof(generationOptions));
            }

            if (versioningOptions == null)
            {
                throw new ArgumentNullException(nameof(versioningOptions));
            }

            var mutableGenerationOptions = new MutableGenerationOptions(generationOptions);
            var mutableVersioningOptions = new MutableVersioningOptions(versioningOptions);

            if (!string.IsNullOrWhiteSpace(solutionFilePath))
            {
                var allFiles      = new EditorConfigParser(".unitTestGeneratorConfig").GetConfigurationFilesTillRoot(solutionFilePath);
                var allProperties = allFiles.SelectMany(x => x.Sections).SelectMany(x => x);
                var properties    = new Dictionary <string, string>();
                foreach (var pair in allProperties)
                {
                    properties[pair.Key] = pair.Value;
                }

                properties.ApplyTo(mutableGenerationOptions);
                properties.ApplyTo(mutableVersioningOptions);
            }

            return(new UnitTestGeneratorOptions(mutableGenerationOptions, mutableVersioningOptions));
        }
Exemplo n.º 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;
 }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 8
0
        public static void Main(string[] args)
        {
            if (args is null)
            {
                throw new ArgumentNullException(nameof(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.ParseMany(arguments.FileNames).ToList();
                if (results.Count == 0)
                {
                    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);
            }
        }
Exemplo n.º 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)));
        }
Exemplo n.º 10
0
 static EditorConfigReader()
 {
     configParser = new EditorConfigParser();
 }
 /// <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());
Exemplo n.º 12
0
 private void OnSolutionLoaded(object sender, SolutionEventArgs e)
 {
     _parser = new EditorConfigParser();
     UpdatePolicy(e.Solution, "text/x-csharp", ".cs");
     UpdatePolicy(e.Solution, "text/plain", "");
 }
Exemplo n.º 13
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;
            }
        }
    }