コード例 #1
0
        /// <summary>
        /// Get the dictionary for the specified buffer
        /// </summary>
        /// <param name="buffer">The buffer for which to get a dictionary</param>
        /// <returns>The spelling dictionary for the buffer or null if one is not provided</returns>
        public SpellingDictionary GetDictionary(ITextBuffer buffer)
        {
            SpellingDictionary service = null;

            if (buffer != null && !buffer.Properties.TryGetProperty(typeof(SpellingDictionary), out service))
            {
                // Get the configuration and create the dictionary based on the configuration
                var config = this.GetConfiguration(buffer);

                if (config != null)
                {
                    // Create or get the existing global dictionary for the default language
                    var globalDictionary = GlobalDictionary.CreateGlobalDictionary(config.DefaultLanguage,
                                                                                   globalServiceProvider, config.AdditionalDictionaryFolders, config.RecognizedWords);

                    if (globalDictionary != null)
                    {
                        service = new SpellingDictionary(globalDictionary, config.IgnoredWords);
                        buffer.Properties[typeof(SpellingDictionary)] = service;
                    }
                }
            }

            return(service);
        }
コード例 #2
0
        /// <summary>
        /// Get the dictionary for the specified buffer
        /// </summary>
        /// <param name="buffer">The buffer for which to get a dictionary</param>
        /// <returns>The spelling dictionary for the buffer or null if one is not provided</returns>
        public SpellingDictionary GetDictionary(ITextBuffer buffer)
        {
            SpellingDictionary service = null;

            if (buffer != null && !buffer.Properties.TryGetProperty(typeof(SpellingDictionary), out service))
            {
                // Get the configuration and create the dictionary based on the configuration
                var config = this.GetConfiguration(buffer);

                if (config != null)
                {
                    // Create a dictionary for each configuration dictionary language ignoring any that are
                    // invalid and duplicates caused by missing languages which return the en-US dictionary.
                    var globalDictionaries = config.DictionaryLanguages.Select(l =>
                                                                               GlobalDictionary.CreateGlobalDictionary(l, globalServiceProvider,
                                                                                                                       config.AdditionalDictionaryFolders, config.RecognizedWords)).Where(
                        d => d != null).Distinct().ToList();

                    if (globalDictionaries.Any())
                    {
                        service = new SpellingDictionary(globalDictionaries, config.IgnoredWords);
                        buffer.Properties[typeof(SpellingDictionary)] = service;
                    }
                }
            }

            return(service);
        }
コード例 #3
0
        //=====================================================================

        /// <summary>
        /// When closed, disconnect event handlers and dispose of resources
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        private void ViewClosed(object sender, EventArgs e)
        {
            _isClosed = true;

            if (_timer != null)
            {
                _timer.Stop();
            }

            if (_buffer != null)
            {
                _buffer.Changed -= BufferChanged;
            }

            if (_naturalTextAggregator != null)
            {
                _naturalTextAggregator.Dispose();
            }

            if (_urlAggregator != null)
            {
                _urlAggregator.Dispose();
            }

            if (_dictionary != null)
            {
                _dictionary.DictionaryUpdated -= DictionaryUpdated;
                _dictionary.ReplaceAll        -= ReplaceAll;
                _dictionary.IgnoreOnce        -= IgnoreOnce;
                _dictionary = null;
            }
        }
コード例 #4
0
        //=====================================================================

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="buffer">The text buffer</param>
        /// <param name="view">The text view</param>
        /// <param name="naturalTextAggregator">The tag aggregator</param>
        /// <param name="urlAggregator">The URL aggregator</param>
        /// <param name="configuration">The spell checker configuration to use</param>
        /// <param name="dictionary">The spelling dictionary to use</param>
        public SpellingTagger(ITextBuffer buffer, ITextView view,
                              ITagAggregator <INaturalTextTag> naturalTextAggregator, ITagAggregator <IUrlTag> urlAggregator,
                              SpellCheckerConfiguration configuration, SpellingDictionary dictionary)
        {
            _isClosed = false;
            _buffer   = buffer;
            _naturalTextAggregator = naturalTextAggregator;
            _urlAggregator         = urlAggregator;
            _dispatcher            = Dispatcher.CurrentDispatcher;
            this.configuration     = configuration;
            _dictionary            = dictionary;

            _dirtySpans      = new List <SnapshotSpan>();
            _misspellings    = new List <MisspellingTag>();
            wordsIgnoredOnce = new List <IgnoredWord>();

            _buffer.Changed += BufferChanged;
            _naturalTextAggregator.TagsChanged += AggregatorTagsChanged;
            _urlAggregator.TagsChanged         += AggregatorTagsChanged;
            _dictionary.DictionaryUpdated      += DictionaryUpdated;
            _dictionary.ReplaceAll             += ReplaceAll;
            _dictionary.IgnoreOnce             += IgnoreOnce;

            view.Closed += ViewClosed;

            // To start with, the entire buffer is dirty.  Split this into chunks so we update pieces at a time.
            ITextSnapshot snapshot = _buffer.CurrentSnapshot;

            foreach (var line in snapshot.Lines)
            {
                AddDirtySpan(line.Extent);
            }
        }
コード例 #5
0
        /// <summary>
        /// This is used to register a spelling dictionary service with the global dictionary so that it is
        /// notified of changes to the global dictionary.
        /// </summary>
        /// <param name="service">The dictionary service to register</param>
        public void RegisterSpellingDictionaryService(SpellingDictionary service)
        {
            // Clear out ones that have been disposed of
            foreach (var svc in registeredServices.Where(s => !s.IsAlive).ToArray())
            {
                registeredServices.Remove(svc);
            }

            System.Diagnostics.Debug.WriteLine("Registered services count: {0}", registeredServices.Count);

            registeredServices.Add(new WeakReference(service));
        }
コード例 #6
0
        //=====================================================================

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="buffer">The text buffer</param>
        /// <param name="view">The text view</param>
        /// <param name="naturalTextAggregator">The tag aggregator</param>
        /// <param name="urlAggregator">The URL aggregator</param>
        /// <param name="configuration">The spell checker configuration to use</param>
        /// <param name="dictionary">The spelling dictionary to use</param>
        public SpellingTagger(ITextBuffer buffer, ITextView view,
                              ITagAggregator <INaturalTextTag> naturalTextAggregator, ITagAggregator <IUrlTag> urlAggregator,
                              SpellCheckerConfiguration configuration, SpellingDictionary dictionary)
        {
            _isClosed = false;
            _buffer   = buffer;
            _naturalTextAggregator = naturalTextAggregator;
            _urlAggregator         = urlAggregator;
            _dispatcher            = Dispatcher.CurrentDispatcher;
            this.configuration     = configuration;
            _dictionary            = dictionary;

            _dirtySpans        = new List <SnapshotSpan>();
            _misspellings      = new List <MisspellingTag>();
            wordsIgnoredOnce   = new List <IgnoredOnceWord>();
            inlineIgnoredWords = new List <InlineIgnoredWord>();

            string filename = buffer.GetFilename();

            wordSplitter = new WordSplitter
            {
                Configuration = configuration,
                Mnemonic      = ClassifierFactory.GetMnemonic(filename),
                IsCStyleCode  = ClassifierFactory.IsCStyleCode(filename)
            };

            _buffer.Changed += BufferChanged;
            _naturalTextAggregator.TagsChanged += AggregatorTagsChanged;
            _urlAggregator.TagsChanged         += AggregatorTagsChanged;
            _dictionary.DictionaryUpdated      += DictionaryUpdated;
            _dictionary.ReplaceAll             += ReplaceAll;
            _dictionary.IgnoreOnce             += IgnoreOnce;

            view.Closed += ViewClosed;

            // Strings in SQL script can contain escaped single quotes which are apostrophes.  Unescape them
            // so that they are spell checked correctly.
            unescapeApostrophes = buffer.ContentType.IsOfType("SQL Server Tools");

            // To start with, the entire buffer is dirty.  Split this into chunks so we update pieces at a time.
            ITextSnapshot snapshot = _buffer.CurrentSnapshot;

            foreach (var line in snapshot.Lines)
            {
                AddDirtySpan(line.Extent);
            }
        }
コード例 #7
0
        /// <summary>
        /// This is used to register a spelling dictionary service with the global dictionary so that it is
        /// notified of changes to the global dictionary.
        /// </summary>
        /// <param name="service">The dictionary service to register</param>
        public void RegisterSpellingDictionaryService(SpellingDictionary service)
        {
            // Clear out ones that have been disposed of
            foreach(var svc in registeredServices.Where(s => !s.IsAlive).ToArray())
                registeredServices.Remove(svc);

            System.Diagnostics.Debug.WriteLine("Registered services count: {0}", registeredServices.Count);

            registeredServices.Add(new WeakReference(service));
        }
コード例 #8
0
        /// <summary>
        /// Get the dictionary for the specified buffer
        /// </summary>
        /// <param name="buffer">The buffer for which to get a dictionary</param>
        /// <returns>The spelling dictionary for the buffer or null if one is not provided</returns>
        public SpellingDictionary GetDictionary(ITextBuffer buffer)
        {
            SpellingDictionary service = null;

            if(buffer != null && !buffer.Properties.TryGetProperty(typeof(SpellingDictionary), out service))
            {
                // Get the configuration and create the dictionary based on the configuration
                var config = this.GetConfiguration(buffer);

                if(config != null)
                {
                    // Create a dictionary for each configuration dictionary language ignoring any that are
                    // invalid and duplicates caused by missing languages which return the en-US dictionary.
                    var globalDictionaries = config.DictionaryLanguages.Select(l =>
                        GlobalDictionary.CreateGlobalDictionary(l, globalServiceProvider,
                        config.AdditionalDictionaryFolders, config.RecognizedWords)).Where(
                        d => d != null).Distinct().ToList();

                    if(globalDictionaries.Any())
                    {
                        service = new SpellingDictionary(globalDictionaries, config.IgnoredWords);
                        buffer.Properties[typeof(SpellingDictionary)] = service;
                    }
                }
            }

            return service;
        }
コード例 #9
0
        /// <summary>
        /// Get the dictionary for the specified buffer
        /// </summary>
        /// <param name="buffer">The buffer for which to get a dictionary</param>
        /// <returns>The spelling dictionary for the buffer or null if one is not provided</returns>
        public SpellingDictionary GetDictionary(ITextBuffer buffer)
        {
            SpellingDictionary service = null;

            if(buffer != null && !buffer.Properties.TryGetProperty(typeof(SpellingDictionary), out service))
            {
                // Get the configuration and create the dictionary based on the configuration
                var config = this.GetConfiguration(buffer);

                if(config != null)
                {
                    // Create or get the existing global dictionary for the default language
                    var globalDictionary = GlobalDictionary.CreateGlobalDictionary(config.DefaultLanguage,
                        globalServiceProvider, config.AdditionalDictionaryFolders, config.RecognizedWords);

                    if(globalDictionary != null)
                    {
                        service = new SpellingDictionary(globalDictionary, config.IgnoredWords);
                        buffer.Properties[typeof(SpellingDictionary)] = service;
                    }
                }
            }

            return service;
        }