public override void Invoke() { var element = HtmlSmartTag.Element; var textBuffer = HtmlSmartTag.TextBuffer; var view = HtmlSmartTag.TextView; var content = textBuffer.CurrentSnapshot.GetText(element.InnerRange.Start, element.InnerRange.Length).Trim(); int start = element.Start; int length = content.Length; EditorExtensionsPackage.DTE.UndoContext.Open(this.DisplayText); using (var edit = textBuffer.CreateEdit()) { edit.Replace(element.Start, element.OuterRange.Length, content); edit.Apply(); } SnapshotSpan span = new SnapshotSpan(view.TextBuffer.CurrentSnapshot, start, length); view.Selection.Select(span, false); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); view.Caret.MoveTo(new SnapshotPoint(view.TextBuffer.CurrentSnapshot, start)); view.Selection.Clear(); EditorExtensionsPackage.DTE.UndoContext.Close(); }
public override void Invoke() { var element = this.HtmlSmartTag.Element; var textBuffer = this.HtmlSmartTag.TextBuffer; int start = this.HtmlSmartTag.Element.Start; int length = this.HtmlSmartTag.Element.Length; string img = textBuffer.CurrentSnapshot.GetText(start, length); string figure = string.Format(_figureHtml, img); EditorExtensionsPackage.DTE.UndoContext.Open(this.DisplayText); using (var edit = textBuffer.CreateEdit()) { edit.Replace(start, length, figure); edit.Apply(); } this.HtmlSmartTag.TextView.Caret.MoveToPreviousCaretPosition(); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); this.HtmlSmartTag.TextView.Caret.MoveToNextCaretPosition(); EditorExtensionsPackage.DTE.UndoContext.Close(); }
private void InsertEmbedString(ITextSnapshot snapshot, string dataUri) { using (EditorExtensionsPackage.UndoContext((DisplayText))) { Declaration dec = _url.FindType <Declaration>(); if (dec != null && dec.Parent != null && !(dec.Parent.Parent is FontFaceDirective)) // No declaration in LESS variable definitions { RuleBlock rule = _url.FindType <RuleBlock>(); string text = dec.Text; if (dec != null && rule != null) { Declaration match = rule.Declarations.FirstOrDefault(d => d.PropertyName != null && d.PropertyName.Text == "*" + dec.PropertyName.Text); if (!text.StartsWith("*", StringComparison.Ordinal) && match == null) { _span.TextBuffer.Insert(dec.AfterEnd, "*" + text + "/* For IE 6 and 7 */"); } } } _span.TextBuffer.Replace(_span.GetSpan(snapshot), dataUri); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); EditorExtensionsPackage.ExecuteCommand("Edit.CollapsetoDefinitions"); } }
protected override bool Execute(CssCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { var point = TextView.GetSelection("CSS"); if (point == null) { return(false); } ITextSnapshot snapshot = point.Value.Snapshot; var doc = CssEditorDocument.FromTextBuffer(snapshot.TextBuffer); StringBuilder sb = new StringBuilder(snapshot.GetText()); int scrollPosition = TextView.TextViewLines.FirstVisibleLine.Extent.Start.Position; using (EditorExtensionsPackage.UndoContext("Remove Duplicate Properties")) { int count; string result = RemoveDuplicateProperties(sb, doc, out count); Span span = new Span(0, snapshot.Length); snapshot.TextBuffer.Replace(span, result); var selection = EditorExtensionsPackage.DTE.ActiveDocument.Selection as TextSelection; selection.GotoLine(1); EditorExtensionsPackage.ExecuteCommand("Edit.FormatDocument"); TextView.ViewScroller.ScrollViewportVerticallyByLines(ScrollDirection.Down, TextView.TextSnapshot.GetLineNumberFromPosition(scrollPosition)); EditorExtensionsPackage.DTE.StatusBar.Text = count + " duplicate properties removed"; } return(true); }
protected override bool Execute(CssCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { var point = TextView.GetSelection("css"); if (point == null) { return(false); } var buffer = point.Value.Snapshot.TextBuffer; int scrollPosition = TextView.TextViewLines.FirstVisibleLine.Extent.Start.Position; using (EditorExtensionsPackage.UndoContext("Sort All Properties")) { string result = SortProperties(buffer.CurrentSnapshot.GetText(), buffer.ContentType); Span span = new Span(0, buffer.CurrentSnapshot.Length); buffer.Replace(span, result); EditorExtensionsPackage.ExecuteCommand("Edit.FormatDocument"); var selection = EditorExtensionsPackage.DTE.ActiveDocument.Selection as TextSelection; selection.GotoLine(1); TextView.ViewScroller.ScrollViewportVerticallyByLines(ScrollDirection.Down, TextView.TextSnapshot.GetLineNumberFromPosition(scrollPosition)); EditorExtensionsPackage.DTE.StatusBar.Text = "Properties sorted"; } return(true); }
protected override bool Execute(CssCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { var point = TextView.GetSelection("css"); if (point == null) { return(false); } ITextBuffer buffer = point.Value.Snapshot.TextBuffer; CssEditorDocument doc = CssEditorDocument.FromTextBuffer(buffer); ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null); StringBuilder sb = new StringBuilder(buffer.CurrentSnapshot.GetText()); int scrollPosition = TextView.TextViewLines.FirstVisibleLine.Extent.Start.Position; using (EditorExtensionsPackage.UndoContext("Add Missing Standard Property")) { int count; string result = AddMissingStandardDeclaration(sb, doc, rootSchema, out count); Span span = new Span(0, buffer.CurrentSnapshot.Length); buffer.Replace(span, result); var selection = EditorExtensionsPackage.DTE.ActiveDocument.Selection as TextSelection; selection.GotoLine(1); EditorExtensionsPackage.ExecuteCommand("Edit.FormatDocument"); TextView.ViewScroller.ScrollViewportVerticallyByLines(ScrollDirection.Down, TextView.TextSnapshot.GetLineNumberFromPosition(scrollPosition)); EditorExtensionsPackage.DTE.StatusBar.Text = count + " missing standard properties added"; } return(true); }
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 InsertEmbedString(ITextSnapshot snapshot, string dataUri) { using (EditorExtensionsPackage.UndoContext((DisplayText))) { _span.TextBuffer.Replace(_span.GetSpan(snapshot), dataUri); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); EditorExtensionsPackage.ExecuteCommand("Edit.CollapsetoDefinitions"); } }
public override void Invoke() { string separator = Microsoft.CSS.Editor.CssSettings.FormatterBlockBracePosition == BracePosition.Compact ? " " : Environment.NewLine; string insert = _lastVendor.Text + separator + _standard.Text; using (EditorExtensionsPackage.UndoContext((DisplayText))) { _span.TextBuffer.Replace(new Span(_lastVendor.Start, _lastVendor.Length), insert); _span.TextBuffer.Delete(new Span(_standard.Start, _standard.Length)); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); } }
public override void Invoke() { string separator = _declaration.Parent.Text.Contains("\r") || _declaration.Parent.Text.Contains("\n") ? Environment.NewLine : " "; int index = _declaration.Text.IndexOf(":", StringComparison.Ordinal); string newDec = _standardName + _declaration.Text.Substring(index); using (EditorExtensionsPackage.UndoContext((DisplayText))) { SnapshotSpan span = _span.GetSpan(_span.TextBuffer.CurrentSnapshot); _span.TextBuffer.Replace(span, _declaration.Text + separator + newDec); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); } }
private void Minify() { string text = _buffer.CurrentSnapshot.GetText(); text = Regex.Replace(text, @"(},|};|}\s+|\),|\);|}(?=\w)|(?=if\())", "$1" + Environment.NewLine); using (EditorExtensionsPackage.UndoContext("Un-Minify")) { Span span = new Span(0, _buffer.CurrentSnapshot.Length); _buffer.Replace(span, text); EditorExtensionsPackage.ExecuteCommand("Edit.FormatDocument"); } }
public override void Invoke() { //string separator = _directive.Parent.Text.Contains("\r") || _directive.Parent.Text.Contains("\n") ? Environment.NewLine : " "; //int index = _directive.Text.IndexOf(":", StringComparison.Ordinal); //string newDec = _standardName + _directive.Text.Substring(index); using (EditorExtensionsPackage.UndoContext((DisplayText))) { //SnapshotSpan span = _span.GetSpan(_span.TextBuffer.CurrentSnapshot); string text = _directive.Text.Replace("@" + _directive.Keyword.Text, _standardName); _span.TextBuffer.Insert(_directive.AfterEnd, Environment.NewLine + Environment.NewLine + text); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); } }
public override void Invoke() { var element = this.HtmlSmartTag.Element; var textBuffer = this.HtmlSmartTag.TextBuffer; EditorExtensionsPackage.DTE.UndoContext.Open(this.DisplayText); using (var edit = textBuffer.CreateEdit()) { Unminify(element, edit); edit.Apply(); } EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); EditorExtensionsPackage.DTE.UndoContext.Close(); }
private void Update(int start, int end) { using (EditorExtensionsPackage.UndoContext("Surround with...")) { using (var edit = _buffer.CreateEdit()) { edit.Insert(end, "</div>"); edit.Insert(start, "<div>"); edit.Apply(); } SnapshotPoint point = new SnapshotPoint(_buffer.CurrentSnapshot, start + 1); _view.Caret.MoveTo(point); _view.Selection.Select(new SnapshotSpan(_buffer.CurrentSnapshot, point, 3), false); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); } }
public override void Invoke() { RuleBlock rule = _declaration.FindType <RuleBlock>(); StringBuilder sb = new StringBuilder(); string separator = rule.Text.Contains("\r") || rule.Text.Contains("\n") ? Environment.NewLine : " "; foreach (var entry in _prefixes) { sb.Append(entry + _declaration.Text + separator); } using (EditorExtensionsPackage.UndoContext((DisplayText))) { _span.TextBuffer.Replace(_span.GetSpan(_span.TextBuffer.CurrentSnapshot), sb.ToString() + _declaration.Text); if (separator == Environment.NewLine) { EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); } } }
protected override bool Execute(ExtractCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { var point = TextView.GetSelection("SCSS"); if (point == null) { return(false); } var buffer = point.Value.Snapshot.TextBuffer; var doc = CssEditorDocument.FromTextBuffer(buffer); ParseItem item = doc.StyleSheet.ItemBeforePosition(point.Value); ParseItem rule = ScssExtractVariableCommandTarget.FindParent(item); int mixinStart = rule.Start; string name = Microsoft.VisualBasic.Interaction.InputBox("Name of the Mixin", "Web Essentials"); if (!string.IsNullOrEmpty(name)) { using (EditorExtensionsPackage.UndoContext(("Extract to mixin"))) { string text = TextView.Selection.SelectedSpans[0].GetText(); buffer.Insert(rule.Start, "@mixin " + name + "() {" + Environment.NewLine + text + Environment.NewLine + "}" + Environment.NewLine + Environment.NewLine); var selection = TextView.Selection.SelectedSpans[0]; TextView.TextBuffer.Replace(selection.Span, "@include " + name + "();"); TextView.Selection.Select(new SnapshotSpan(TextView.TextBuffer.CurrentSnapshot, mixinStart, 1), false); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); TextView.Selection.Clear(); } return(true); } return(false); }
protected override bool Execute(CssCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { if (_point == null) { return(false); } ITextBuffer buffer = _point.Value.Snapshot.TextBuffer; CssEditorDocument doc = CssEditorDocument.FromTextBuffer(buffer); ICssSchemaInstance rootSchema = CssSchemaManager.SchemaManager.GetSchemaRoot(null); StringBuilder sb = new StringBuilder(buffer.CurrentSnapshot.GetText()); int scrollPosition = TextView.TextViewLines.FirstVisibleLine.Extent.Start.Position; using (EditorExtensionsPackage.UndoContext("Add Missing Standard Property")) { int count; bool hasChanged = AddMissingStandardDeclaration(sb, doc, rootSchema, out count); if (hasChanged) { buffer.SetText(sb.ToString() .Replace("/* BEGIN EXTERNAL SOURCE */\r\n", string.Empty) .Replace("\r\n/* END EXTERNAL SOURCE */\r\n", string.Empty)); } if (TextView.Caret.Position.BufferPosition.Snapshot == buffer.CurrentSnapshot) { (EditorExtensionsPackage.DTE.ActiveDocument.Selection as TextSelection).GotoLine(1); } EditorExtensionsPackage.ExecuteCommand("Edit.FormatDocument"); TextView.ViewScroller.ScrollViewportVerticallyByLines(ScrollDirection.Down, TextView.TextSnapshot.GetLineNumberFromPosition(scrollPosition)); EditorExtensionsPackage.DTE.StatusBar.Text = count + " missing standard properties added"; } return(true); }
private bool InvokeZenCoding() { Span zenSpan = GetText(); if (zenSpan.Length == 0 || TextView.Selection.SelectedSpans[0].Length > 0 || !IsValidTextBuffer()) { return(false); } string zenSyntax = TextView.TextBuffer.CurrentSnapshot.GetText(zenSpan); Parser parser = new Parser(); string result = parser.Parse(zenSyntax, ZenType.HTML); if (!string.IsNullOrEmpty(result)) { Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => { using (EditorExtensionsPackage.UndoContext("ZenCoding")) { ITextSelection selection = UpdateTextBuffer(zenSpan, result); Span newSpan = new Span(zenSpan.Start, selection.SelectedSpans[0].Length); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); SetCaret(newSpan, false); selection.Clear(); } }), DispatcherPriority.ApplicationIdle, null); return(true); } return(false); }
private void UpdateTextBuffer(string fileName) { int position = TextView.Caret.Position.BufferPosition.Position; string relative = FileHelpers.RelativePath(ProjectHelpers.GetRootFolder() ?? "/", fileName); string text = string.Format(CultureInfo.InvariantCulture, _format, relative); using (EditorExtensionsPackage.UndoContext("Insert Image")) { TextView.TextBuffer.Insert(position, text); try { SnapshotSpan span = new SnapshotSpan(TextView.TextBuffer.CurrentSnapshot, position, _format.Length); TextView.Selection.Select(span, false); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); TextView.Selection.Clear(); } catch { // Try to format the selection. Some editors handle this differently, so try/catch } } }
public override void Invoke() { Span ruleSpan = new Span(_rule.Start, _rule.Length); Sorter sorter = new Sorter(); string result = null; if (_view.TextBuffer.ContentType.IsOfType("LESS")) { result = sorter.SortLess(_rule.Text); } else { result = sorter.SortStyleSheet(_rule.Text); } var position = _view.Caret.Position.BufferPosition; using (EditorExtensionsPackage.UndoContext((DisplayText))) { _span.TextBuffer.Replace(ruleSpan, result); _view.Caret.MoveTo(new SnapshotPoint(position.Snapshot.TextBuffer.CurrentSnapshot, position)); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); } }