private static async Task UpdateSheetRulesAsync(string file, IEnumerable <CssSelectorChangeData> set) { ////Get off the UI thread //await Task.Factory.StartNew(() => { }); var doc = DocumentFactory.GetDocument(file, true); if (doc == null) { return; } var oldSnapshotOnChange = doc.IsProcessingUnusedCssRules; var window = EditorExtensionsPackage.DTE.ItemOperations.OpenFile(file); window.Activate(); var buffer = ProjectHelpers.GetCurentTextBuffer(); var flattenedRules = FlattenRules(doc); var allEdits = new List <CssRuleBlockSyncAction>(); doc.IsProcessingUnusedCssRules = false; doc.Reparse(buffer.CurrentSnapshot.GetText()); foreach (var item in set) { var selectorName = RuleRegistry.StandardizeSelector(item.Rule); var matchingRules = flattenedRules.Where(x => string.Equals(x.CleansedSelectorName, selectorName, StringComparison.Ordinal)).OrderBy(x => x.Offset).ToList(); var rule = matchingRules[item.RuleIndex]; var actions = await CssRuleDefinitionSync.ComputeSyncActionsAsync(rule.Source, item.NewValue, item.OldValue); allEdits.AddRange(actions); } var compositeEdit = buffer.CreateEdit(); try { foreach (var action in allEdits) { action(window, compositeEdit); } } catch { compositeEdit.Cancel(); } finally { if (!compositeEdit.Canceled) { compositeEdit.Apply(); EditorExtensionsPackage.ExecuteCommand("Edit.FormatDocument"); window.Document.Save(); } } //await Task.Delay(2000); //<-- Try to wait for the files to actually save doc.IsProcessingUnusedCssRules = oldSnapshotOnChange; }
private void BufferOnPostChanged(object sender, EventArgs eventArgs) { var fileName = _buffer.GetFileName(); if (fileName == null) { return; } var doc = DocumentFactory.GetDocument(fileName); if (doc == null) { return; } doc.Reparse(_buffer.CurrentSnapshot.GetText()); UsageRegistry.Resynchronize(); }
public IEnumerable <ITagSpan <IErrorTag> > GetTags(NormalizedSnapshotSpanCollection spans) { var fileName = _buffer.GetFileName(); if (fileName == null) { return(new ITagSpan <UnusedCssTag> [0]); } var doc = DocumentFactory.GetDocument(fileName); if (doc == null) { return(new ITagSpan <UnusedCssTag> [0]); } var result = new List <ITagSpan <IErrorTag> >(); using (AmbientRuleContext.GetOrCreate()) { var applicableRules = UsageRegistry.GetAllUnusedRules(new HashSet <IStylingRule>(doc.Rules)); foreach (var rule in applicableRules) { try { result.Add(UnusedCssTag.FromRuleSet(_buffer, rule)); } catch (ArgumentOutOfRangeException) { //Sometimes while the document is being modified and retagging is happening, a rule's boundary will momentarily be out of sync with the document and possibly past the end // this is a condition that may be safely ignored } } } return(result); }