Example #1
0
        internal static void AddStyle(IStreamProvider provider)
        {
            string     styleName;
            JSonFormat format;

            using (var stream = provider.Open()) {
                if (TryScanJSonStyle(stream, out styleName, out format))
                {
                    switch (format)
                    {
                    case JSonFormat.OldSyntaxTheme:
                        var theme = OldFormat.ImportColorScheme(provider.Open());
                        if (theme != null)
                        {
                            builtInBundle.Add(theme);
                        }
                        break;

                    case JSonFormat.TextMateJsonSyntax:
                        SyntaxHighlightingDefinition highlighting = TextMateFormat.ReadHighlightingFromJson(provider.Open());
                        if (highlighting != null)
                        {
                            builtInBundle.Add(highlighting);
                        }
                        break;
                    }
                }
            }
        }
 public SyntaxHighlighting(SyntaxHighlightingDefinition definition, IReadonlyTextDocument document)
 {
     this.definition = definition ?? throw new ArgumentNullException(nameof(definition));
     Document        = document;
     if (document is ITextDocument)
     {
         ((ITextDocument)document).TextChanged += Handle_TextChanged;
     }
 }
Example #3
0
 public SyntaxHighlighting(SyntaxHighlightingDefinition definition, IReadonlyTextDocument document)
 {
     this.definition = definition;
     Document        = document;
     if (document is ITextDocument)
     {
         ((ITextDocument)document).TextChanged += Handle_TextChanged;
     }
 }
        public SyntaxHighlightingDefinition GetSyntaxHighlightingDefinition()
        {
            if (syntaxHighlightingDefinition != null)
            {
                return(syntaxHighlightingDefinition);
            }

            syntaxHighlightingDefinition = LoadSyntaxHighlightingDefinition(getStreamProvider().Open());
            syntaxHighlightingDefinition.PrepareMatches();
            return(syntaxHighlightingDefinition);
        }
        static SyntaxHighlightingDefinition GetSyntaxHighlightingDefinitionByName(FilePath fileName)
        {
            SyntaxHighlightingDefinition bestMatch = null;

            string bestType     = null;
            int    bestPosition = 0;
            var    fileNameStr  = (string)fileName;
            var    name         = fileName.FileName;

            foreach (var bundle in languageBundles)
            {
                foreach (var h in bundle.Highlightings)
                {
                    for (int i = 0; i < h.FileTypes.Count; i++)
                    {
                        var fileType = h.FileTypes [i];
                        if (!fileNameStr.EndsWith(fileType, StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }

                        //if type is full match to filename e.g. ChangeLog, we're done
                        if (name.Length == fileType.Length)
                        {
                            return(h);
                        }

                        //if type didn't start with period, check filename has one in the right place
                        //e.g type 'y' is not valid match for name 'ab.xy'
                        if (fileType [0] != '.' && fileNameStr [fileNameStr.Length - fileType.Length - 1] != '.')
                        {
                            continue;
                        }

                        if (bestType == null ||                                       // 1st match we take anything
                            bestType.Length < fileType.Length ||                      // longer match is better, e.g. 'xy' matches 'ab.nm.xy', but 'nm.xy' matches better
                            (bestType.Length == fileType.Length && bestPosition > i)) //fileType is same... take higher on list(e.g. XAML specific will have .xaml at index 0, but XML general will have .xaml at index 68)
                        {
                            bestType     = fileType;
                            bestPosition = i;
                            bestMatch    = h;
                        }
                    }
                }
            }

            return(bestMatch);
        }
 internal void SetDefinition(SyntaxHighlightingDefinition definition)
 {
     this.definition = definition;
     foreach (var o in includesAndMatches)
     {
         var match = o as SyntaxMatch;
         if (match != null)
         {
             if (match.Push is AnonymousMatchContextReference)
             {
                 ((AnonymousMatchContextReference)match.Push).Context.SetDefinition(definition);
             }
             if (match.Set is AnonymousMatchContextReference)
             {
                 ((AnonymousMatchContextReference)match.Set).Context.SetDefinition(definition);
             }
         }
     }
 }
Example #7
0
        static SyntaxHighlightingDefinition GetSyntaxHighlightingDefinitionByName(FilePath fileName)
        {
            SyntaxHighlightingDefinition bestMatch = null;

            string foundType   = null;
            var    fileNameStr = (string)fileName;
            var    name        = fileName.FileName;

            foreach (var bundle in languageBundles)
            {
                foreach (var h in bundle.Highlightings)
                {
                    foreach (var fileType in h.FileTypes)
                    {
                        if (!fileNameStr.EndsWith(fileType, StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }

                        //if type is full match to filename e.g. ChangeLog, we're done
                        if (name.Length == fileType.Length)
                        {
                            return(h);
                        }

                        //if type didn't start with period, check filename has one in the right place
                        //e.g type 'y' is not valid match for name 'ab.xy'
                        if (fileType [0] != '.' && fileNameStr [fileNameStr.Length - fileType.Length - 1] != '.')
                        {
                            continue;
                        }

                        //we have a valid match. but there may be a better/longer one
                        //e.g. 'xy' matches 'ab.nm.xy', but 'nm.xy' matches better
                        foundType = fileType;
                        bestMatch = h;
                    }
                }
            }

            return(bestMatch);
        }
        /*
         *              if (provider.Name.EndsWith (".vssettings", StringComparison.Ordinal)) {
         *              styles [name] = OldFormat.ImportVsSetting (provider.Name, stream);
         *      } else if (provider.Name.EndsWith (".json", StringComparison.Ordinal)) {
         *              styles [name] = OldFormat.ImportColorScheme (stream);
         *      } else {
         *              styles [name] = TextMateFormat.LoadEditorTheme (stream);
         *      }
         *      styles [name].FileName = provider.Name;
         *
         * */

        static object LoadFile(LanguageBundle bundle, string file, Func <Stream> openStream, Func <IStreamProvider> getStreamProvider)
        {
            if (file.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
            {
                using (var stream = openStream()) {
                    string     styleName;
                    JSonFormat format;
                    if (TryScanJSonStyle(stream, out styleName, out format))
                    {
                        switch (format)
                        {
                        case JSonFormat.OldSyntaxTheme:
                            var theme = OldFormat.ImportColorScheme(getStreamProvider().Open());
                            if (theme != null)
                            {
                                bundle.Add(theme);
                            }
                            return(theme);

                        case JSonFormat.TextMateJsonSyntax:
                            SyntaxHighlightingDefinition highlighting = TextMateFormat.ReadHighlightingFromJson(getStreamProvider().Open());
                            if (highlighting != null)
                            {
                                bundle.Add(highlighting);
                            }
                            return(highlighting);
                        }
                    }
                }
            }
            else if (file.EndsWith(".tmTheme", StringComparison.OrdinalIgnoreCase))
            {
                using (var stream = openStream()) {
                    string styleName = ScanTextMateStyle(stream);
                    if (!string.IsNullOrEmpty(styleName))
                    {
                        var theme = TextMateFormat.LoadEditorTheme(getStreamProvider().Open());
                        if (theme != null)
                        {
                            bundle.Add(theme);
                        }
                        return(theme);
                    }
                    else
                    {
                        LoggingService.LogError("Invalid .tmTheme theme file : " + file);
                    }
                }
            }
            else if (file.EndsWith(".vssettings", StringComparison.OrdinalIgnoreCase))
            {
                using (var stream = openStream()) {
                    string      styleName = Path.GetFileNameWithoutExtension(file);
                    EditorTheme theme;
                    try {
                        theme = OldFormat.ImportVsSetting(styleName, getStreamProvider().Open());
                    } catch (StyleImportException e) {
                        switch (e.Reason)
                        {
                        case StyleImportException.ImportFailReason.Unknown:
                            LoggingService.LogWarning("Unknown error in theme file : " + file, e);
                            break;

                        case StyleImportException.ImportFailReason.NoValidColorsFound:
                            LoggingService.LogWarning("No colors defined in vssettings : " + file, e);
                            break;
                        }
                        return(null);
                    } catch (Exception e) {
                        LoggingService.LogWarning("Invalid theme : " + file, e);
                        return(null);
                    }
                    if (theme != null)
                    {
                        bundle.Add(theme);
                    }
                    return(theme);
                }
            }
            else if (file.EndsWith(".tmLanguage", StringComparison.OrdinalIgnoreCase))
            {
                using (var stream = openStream()) {
                    var highlighting = TextMateFormat.ReadHighlighting(stream);
                    if (highlighting != null)
                    {
                        bundle.Add(highlighting);
                    }
                    return(highlighting);
                }
            }
            else if (file.EndsWith(".sublime-syntax", StringComparison.OrdinalIgnoreCase))
            {
                using (var stream = new StreamReader(openStream())) {
                    var highlighting = Sublime3Format.ReadHighlighting(stream);
                    if (highlighting != null)
                    {
                        bundle.Add(highlighting);
                    }
                    return(highlighting);
                }
            }
            else if (file.EndsWith(".sublime-package", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".tmbundle", StringComparison.OrdinalIgnoreCase))
            {
                try {
                    using (var stream = new ICSharpCode.SharpZipLib.Zip.ZipInputStream(openStream())) {
                        var entry     = stream.GetNextEntry();
                        var newBundle = new LanguageBundle(Path.GetFileNameWithoutExtension(file), file);
                        while (entry != null)
                        {
                            if (entry.IsFile && !entry.IsCrypted)
                            {
                                if (stream.CanDecompressEntry)
                                {
                                    byte [] data = new byte [entry.Size];
                                    stream.Read(data, 0, (int)entry.Size);
                                    LoadFile(newBundle, entry.Name, () => new MemoryStream(data), () => new MemoryStreamProvider(data, entry.Name));
                                }
                            }
                            entry = stream.GetNextEntry();
                        }
                        languageBundles.Add(newBundle);
                        return(newBundle);
                    }
                } catch (Exception e) {
                    LoggingService.LogError("Error while reading : " + file, e);
                }
            }
            else if (file.EndsWith(".tmPreferences", StringComparison.OrdinalIgnoreCase))
            {
                using (var stream = openStream()) {
                    var preference = TextMateFormat.ReadPreferences(stream);
                    if (preference != null)
                    {
                        bundle.Add(preference);
                    }
                    return(preference);
                }
            }
            else if (file.EndsWith(".tmSnippet", StringComparison.OrdinalIgnoreCase))
            {
                using (var stream = openStream()) {
                    var snippet = TextMateFormat.ReadSnippet(stream);
                    if (snippet != null)
                    {
                        bundle.Add(snippet);
                    }
                    return(snippet);
                }
            }
            else if (file.EndsWith(".sublime-snippet", StringComparison.OrdinalIgnoreCase))
            {
                using (var stream = openStream()) {
                    var snippet = Sublime3Format.ReadSnippet(stream);
                    if (snippet != null)
                    {
                        bundle.Add(snippet);
                    }
                    return(snippet);
                }
            }
            return(null);
        }
Example #9
0
 public void Add(SyntaxHighlightingDefinition highlighting)
 {
     highlightings.Add(highlighting);
 }