|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Tagging;
namespace DigitalSamurai.SpellSharp.Vsx
{
public partial class SpellingTag : IErrorTag
{
public const string Identifier = "Spelling error";
public IServiceProvider ServiceProvider { get; private set; }
public ITrackingSpan TrackingSpan { get; private set; }
public string MisspelledWord { get; private set; }
#region public string ErrorType { get; }
public string ErrorType
{
get { return Identifier; }
}
#endregion
#region public object ToolTipContent { get; }
public object ToolTipContent
{
get
{
return string.Format
(
"The spelling of the word '{0}' is not recognized.",
MisspelledWord
);
}
}
#endregion
#region public IEnumerable<string> Suggestions { get (jit); }
private IEnumerable<string> _Suggestions;
public IEnumerable<string> Suggestions
{
get
{
if (_Suggestions == null)
{
_Suggestions = CreateSuggestions ();
}
return _Suggestions;
}
}
#endregion
public SpellingTag (IServiceProvider serviceProvider, ITrackingSpan trackingSpan, string misspelledWord)
{
if (serviceProvider == null)
{
throw new ArgumentNullException ("serviceProvider");
}
if (trackingSpan == null)
{
throw new ArgumentNullException ("trackingSpan");
}
if (string.IsNullOrEmpty (misspelledWord))
{
throw new ArgumentNullException ("misspelledWord");
}
ServiceProvider = serviceProvider;
TrackingSpan = trackingSpan;
MisspelledWord = misspelledWord;
}
public ITagSpan<SpellingTag> CreateMappedTagSpanWhenIntersects (NormalizedSnapshotSpanCollection spans)
{
if (spans == null)
{
throw new ArgumentNullException ("spans");
}
if (spans.Count == 0)
{
return null;
}
var mappedSpan = TrackingSpan.GetSpan (spans[0].Snapshot);
if ((mappedSpan.Length == 0) || !spans.IntersectsWith (new NormalizedSnapshotSpanCollection (mappedSpan)))
{
return null;
}
return new TagSpan<SpellingTag> (mappedSpan, this);
}
private IEnumerable<string> CreateSuggestions ()
{
var suggestions = new List<string> ();
foreach (var suggestion in Hunspell.Library.DetermineSuggestions (MisspelledWord))
{
if (string.IsNullOrWhiteSpace (suggestion))
{
continue;
}
string identifier;
if (CanConcatenateToDotNetIdentifier (suggestion, out identifier))
{
suggestions.Add (identifier);
}
suggestions.Add (suggestion);
}
suggestions.Sort ();
return suggestions;
}
private bool CanConcatenateToDotNetIdentifier (string suggestion, out string identifier)
{
var sections = suggestion.Split (' ');
if (sections.Length == 1)
{
identifier = null;
return false;
}
var output = new StringBuilder (sections[0]);
foreach (var current in sections.Skip (1))
{
if (string.IsNullOrWhiteSpace (current))
{
continue;
}
output.Append (char.ToUpperInvariant (current[0]));
if (current.Length > 1)
{
output.Append (current.Substring (1));
}
}
identifier = output.ToString ();
return true;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.