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 declarations = _rule.Block.Declarations.OrderBy(d => d.PropertyName, new DeclarationComparer()); var position = _view.Caret.Position.BufferPosition.Position; EditorExtensionsPackage.DTE.UndoContext.Open(DisplayText); _span.TextBuffer.Replace(ruleSpan, result);// string.Concat(declarations.Select(d => d.Text))); _view.Caret.MoveTo(new SnapshotPoint(_span.TextBuffer.CurrentSnapshot, position)); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); EditorExtensionsPackage.DTE.UndoContext.Close(); }
protected override bool Execute(MinifyCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { if (_spansTuple == null) { return(false); } var source = _spansTuple.Item2.GetText(); string result = Mef.GetImport <IFileMinifier>(_spansTuple.Item2.Snapshot.TextBuffer.ContentType) .MinifyString(source); if (result == null) { return(false); // IFileMinifier already displayed an error } if (result == source) { EditorExtensionsPackage.DTE.StatusBar.Text = "The selection is already minified"; return(false); } using (EditorExtensionsPackage.UndoContext("Minify")) TextView.TextBuffer.Replace(_spansTuple.Item1, result); return(true); }
public static void AddHierarchyItem(this ErrorTask task) { IVsHierarchy hierarchyItem = null; IVsSolution solution = EditorExtensionsPackage.GetGlobalService <IVsSolution>(typeof(SVsSolution)); Project project = ProjectHelpers.GetActiveProject(); if (solution != null && project != null) { int flag = -1; try { flag = solution.GetProjectOfUniqueName(project.FullName, out hierarchyItem); } catch (COMException ex) { if ((uint)ex.ErrorCode != DISP_E_MEMBERNOTFOUND) { throw; } } if (0 == flag) { task.HierarchyItem = hierarchyItem; } } }
private bool Indent() { int position = _textView.Caret.Position.BufferPosition.Position; if (position == 0 || position == _textView.TextBuffer.CurrentSnapshot.Length || _textView.Selection.SelectedSpans[0].Length > 0) { return(false); } char before = _textView.TextBuffer.CurrentSnapshot.GetText(position - 1, 1)[0]; char after = _textView.TextBuffer.CurrentSnapshot.GetText(position, 1)[0]; if (before == '{' && after == '}') { EditorExtensionsPackage.DTE.UndoContext.Open("Smart indent"); _textView.TextBuffer.Insert(position, Environment.NewLine + '\t'); SnapshotPoint point = new SnapshotPoint(_textView.TextBuffer.CurrentSnapshot, position); _textView.Selection.Select(new SnapshotSpan(point, 4), true); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); _textView.Selection.Clear(); SendKeys.Send("{ENTER}"); EditorExtensionsPackage.DTE.UndoContext.Close(); return(true); } return(false); }
private void UpdateTextBuffer(SnapshotSpan span, string text) { using (EditorExtensionsPackage.UndoContext("Comment/Uncomment")) { TextView.TextBuffer.Replace(span.Span, text.Trim()); } }
private void InsertEmbedString(ITextSnapshot snapshot, string dataUri) { EditorExtensionsPackage.DTE.UndoContext.Open(DisplayText); Declaration dec = _url.FindType <Declaration>(); if (dec != null && dec.Parent != null && !(dec.Parent.Parent is FontFaceDirective)) // No declartion 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("*") && 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"); EditorExtensionsPackage.DTE.UndoContext.Close(); }
private bool Replace(Replacement callback) { TextDocument document = GetTextDocument(); string replacement = callback(document.Selection.Text); using (EditorExtensionsPackage.UndoContext((callback.Method.Name))) document.Selection.Insert(replacement, 0); return(true); }
private void InsertEmbedString(ITextSnapshot snapshot, string dataUri) { EditorExtensionsPackage.DTE.UndoContext.Open(DisplayText); Declaration dec = _url.FindType <Declaration>(); _span.TextBuffer.Replace(_span.GetSpan(snapshot), dataUri); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); EditorExtensionsPackage.ExecuteCommand("Edit.CollapsetoDefinitions"); EditorExtensionsPackage.DTE.UndoContext.Close(); }
public void Redo() { try { EditorExtensionsPackage.ExecuteCommand("Edit.Redo"); EditorExtensionsPackage.DTE.ActiveDocument.Save(); } catch { // Do nothing } }
private ITextSelection UpdateTextBuffer(Span zenSpan, string result) { TextView.TextBuffer.Replace(zenSpan, result); SnapshotPoint point = new SnapshotPoint(TextView.TextBuffer.CurrentSnapshot, zenSpan.Start); SnapshotSpan snapshot = new SnapshotSpan(point, result.Length); TextView.Selection.Select(snapshot, false); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); return(TextView.Selection); }
private bool HandleStartLines(int position, string indentation) { string result = Environment.NewLine + indentation + "* "; using (EditorExtensionsPackage.UndoContext("Smart Indent")) { TextView.TextBuffer.Insert(position, result); SnapshotPoint point = new SnapshotPoint(TextView.TextBuffer.CurrentSnapshot, position + result.Length); TextView.Caret.MoveTo(point); } return(true); }
private void Replace(Replacement callback) { TextDocument document = GetTextDocument(); if (document == null) { return; } string replacement = callback(document.Selection.Text); using (EditorExtensionsPackage.UndoContext((callback.Method.Name))) document.Selection.Insert(replacement, 0); }
private static void UpdateBuffer(string innerHTML, HtmlEditorDocument html, Span span) { using (EditorExtensionsPackage.UndoContext("Design Mode changes")) { try { html.TextBuffer.Replace(span, innerHTML); EditorExtensionsPackage.DTE.ActiveDocument.Save(); } catch { // Do nothing } } }
public void AddHierarchyItem(ErrorTask task) { IVsHierarchy HierarchyItem; IVsSolution solution = EditorExtensionsPackage.GetGlobalService <IVsSolution>(typeof(SVsSolution)); if (solution != null) { int flag = solution.GetProjectOfUniqueName(_connection.Project.FullName, out HierarchyItem); if (0 == flag) { task.HierarchyItem = HierarchyItem; } } }
public override void Invoke() { StringBuilder sb = new StringBuilder(); foreach (var entry in _missingPseudos) { string text = _selector.Text.Replace(_pseudo.Text, entry).Trim(','); sb.Append(text + "," + Environment.NewLine); } EditorExtensionsPackage.DTE.UndoContext.Open(DisplayText); _span.TextBuffer.Insert(_selector.Start, sb.ToString()); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); EditorExtensionsPackage.DTE.UndoContext.Close(); }
protected override bool Execute(ExtractCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { if (TextView == null) { return(false); } string content = TextView.Selection.SelectedSpans[0].GetText(); string extension = Path.GetExtension(_dte.ActiveDocument.FullName).ToLowerInvariant(); if (!_possible.Contains(extension.ToUpperInvariant())) { extension = ".css"; } string name = Interaction.InputBox("Specify the name of the file", "Web Essentials", "file1" + extension).Trim(); if (!string.IsNullOrEmpty(name)) { if (string.IsNullOrEmpty(Path.GetExtension(name))) { name = name + extension; } string fileName = Path.Combine(Path.GetDirectoryName(_dte.ActiveDocument.FullName), name); if (!File.Exists(fileName)) { using (EditorExtensionsPackage.UndoContext("Extract to file...")) { using (StreamWriter writer = new StreamWriter(fileName, false, new UTF8Encoding(true))) { writer.Write(content); } ProjectHelpers.AddFileToActiveProject(fileName); TextView.TextBuffer.Replace(TextView.Selection.SelectedSpans[0].Span, string.Format(CultureInfo.CurrentCulture, "@import \"{0}\";", name)); _dte.ItemOperations.OpenFile(fileName); } } else { Logger.ShowMessage("The file already exists."); } } return(true); }
protected override void Initialize() { base.Initialize(); Instance = this; JsDocComments.Register(); OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService; if (null != mcs) { HandleMenuVisibility(mcs); TransformMenu transform = new TransformMenu(DTE, mcs); transform.SetupCommands(); DiffMenu diffMenu = new DiffMenu(DTE, mcs); diffMenu.SetupCommands(); MinifyFileMenu minifyMenu = new MinifyFileMenu(DTE, mcs); minifyMenu.SetupCommands(); BundleFilesMenu bundleMenu = new BundleFilesMenu(DTE, mcs); bundleMenu.SetupCommands(); JsHintMenu jsHintMenu = new JsHintMenu(DTE, mcs); jsHintMenu.SetupCommands(); ProjectSettingsMenu projectSettingsMenu = new ProjectSettingsMenu(DTE, mcs); projectSettingsMenu.SetupCommands(); SolutionColorsMenu solutionColorsMenu = new SolutionColorsMenu(DTE, mcs); solutionColorsMenu.SetupCommands(); BuildMenu buildMenu = new BuildMenu(DTE, mcs); buildMenu.SetupCommands(); MarkdownStylesheetMenu markdownMenu = new MarkdownStylesheetMenu(DTE, mcs); markdownMenu.SetupCommands(); } // Hook up event handlers Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => { DTE.Events.BuildEvents.OnBuildDone += BuildEvents_OnBuildDone; DTE.Events.SolutionEvents.Opened += delegate { Settings.UpdateCache(); Settings.UpdateStatusBar("applied"); }; DTE.Events.SolutionEvents.AfterClosing += delegate { DTE.StatusBar.Clear(); }; }), DispatcherPriority.ApplicationIdle, null); }
public static void AddHierarchyItem(this ErrorTask task) { IVsHierarchy HierarchyItem; IVsSolution solution = EditorExtensionsPackage.GetGlobalService <IVsSolution>(typeof(SVsSolution)); Project project = ProjectHelpers.GetActiveProject(); if (solution != null && project != null) { int flag = solution.GetProjectOfUniqueName(project.FullName, out HierarchyItem); if (0 == flag) { task.HierarchyItem = HierarchyItem; } } }
protected override bool Execute(LinesCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { var span = GetSpan(); var lines = span.GetText().Split(new[] { Environment.NewLine }, StringSplitOptions.None); if (lines.Length == 0) { return(false); } string result = RemoveDuplicates(lines); using (EditorExtensionsPackage.UndoContext(("Remove Duplicate Lines"))) TextView.TextBuffer.Replace(span.Span, result); return(true); }
private static void AddMetaTag(int index) { Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => { var view = ProjectHelpers.GetCurentTextView(); var buffer = view.TextBuffer; using (EditorExtensionsPackage.UndoContext("Adding <meta> viewport")) { buffer.Insert(index, "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />" + Environment.NewLine); view.Caret.MoveTo(new SnapshotPoint(buffer.CurrentSnapshot, index + 31 + 37)); view.Selection.Select(new SnapshotSpan(buffer.CurrentSnapshot, 31 + index, 37), false); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); } EditorExtensionsPackage.DTE.ActiveDocument.Save(); }), DispatcherPriority.ApplicationIdle, null); }
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); } EditorExtensionsPackage.DTE.UndoContext.Open(DisplayText); _span.TextBuffer.Replace(_span.GetSpan(_span.TextBuffer.CurrentSnapshot), sb.ToString() + _declaration.Text); if (separator == Environment.NewLine) { EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); } EditorExtensionsPackage.DTE.UndoContext.Close(); }
private void Update(int start, int end) { EditorExtensionsPackage.DTE.UndoContext.Open("Surround with..."); using (var edit = _buffer.CreateEdit()) { edit.Insert(end, "</p>"); edit.Insert(start, "<p>"); edit.Apply(); } SnapshotPoint point = new SnapshotPoint(_buffer.CurrentSnapshot, start + 1); _view.Caret.MoveTo(point); _view.Selection.Select(new SnapshotSpan(_buffer.CurrentSnapshot, point, 1), false); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); EditorExtensionsPackage.DTE.UndoContext.Close(); }
private static void AddMetaTag(int index) { Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => { var view = ProjectHelpers.GetCurentTextView(); var buffer = view.TextBuffer; using (EditorExtensionsPackage.UndoContext("Adding <meta> description")) { buffer.Insert(index, "<meta name=\"description\" content=\"The description of my page\" />" + Environment.NewLine); view.Caret.MoveTo(new SnapshotPoint(buffer.CurrentSnapshot, index + 34 + 26)); view.Selection.Select(new SnapshotSpan(buffer.CurrentSnapshot, 34 + index, 26), false); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); } EditorExtensionsPackage.DTE.ActiveDocument.Save(); view.ViewScroller.EnsureSpanVisible(new SnapshotSpan(buffer.CurrentSnapshot, index, 1), EnsureSpanVisibleOptions.AlwaysCenter); }), DispatcherPriority.ApplicationIdle, null); }
private bool Jump() { if (!EnsureInitialized()) { return(false); } int position = _textView.Caret.Position.BufferPosition.Position; ParseItem item = _tree.StyleSheet.ItemBeforePosition(position); if (item != null) { RuleBlock rule = item.FindType <RuleBlock>(); Declaration dec = item.FindType <Declaration>(); if (rule != null && dec != null) { CommitStatementCompletion(); var line = _textView.TextSnapshot.GetLineFromPosition(position); string text = line.Extent.GetText().TrimEnd(); if (!string.IsNullOrWhiteSpace(text) && !text.EndsWith(";")) { using (ITextEdit edit = _textView.TextBuffer.CreateEdit()) { edit.Replace(line.Extent, text + ";"); edit.Apply(); } } EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); SnapshotPoint point = new SnapshotPoint(_textView.TextBuffer.CurrentSnapshot, rule.AfterEnd); _textView.Caret.MoveTo(point); _textView.ViewScroller.EnsureSpanVisible(new SnapshotSpan(point, 0)); return(true); } } return(false); }
public void AddHierarchyItem(ErrorTask task) { if (task == null || Connection == null || Connection.Project == null || string.IsNullOrEmpty(Connection.Project.FullName)) { return; } IVsHierarchy HierarchyItem; IVsSolution solution = EditorExtensionsPackage.GetGlobalService <IVsSolution>(typeof(SVsSolution)); if (solution != null) { int flag = solution.GetProjectOfUniqueName(Connection.Project.FullName, out HierarchyItem); if (0 == flag) { task.HierarchyItem = HierarchyItem; } } }
private bool CompleteComment() { int position = TextView.Caret.Position.BufferPosition.Position; if (position < 1) { return(false); } SnapshotSpan span = new SnapshotSpan(TextView.TextBuffer.CurrentSnapshot, position - 1, 1); bool isComment = _classifier.GetClassificationSpans(span).Any(c => c.ClassificationType.IsOfType("comment")); if (isComment) { return(false); } char prevChar = TextView.TextBuffer.CurrentSnapshot.ToCharArray(position - 1, 1)[0]; // Abort if the previous characters isn't a forward-slash if (prevChar != '/' || isComment) { return(false); } // Insert the typed character TextView.TextBuffer.Insert(position, "*"); using (EditorExtensionsPackage.UndoContext("Comment completion")) { // Use separate undo context for this, so it can be undone separately. TextView.TextBuffer.Insert(position + 1, "*/"); } // Move the caret to the correct point SnapshotPoint point = new SnapshotPoint(TextView.TextBuffer.CurrentSnapshot, position + 1); TextView.Caret.MoveTo(point); return(true); }
public static void OpenFileInPreviewTab(string file) { IVsNewDocumentStateContext newDocumentStateContext = null; try { IVsUIShellOpenDocument3 openDoc3 = EditorExtensionsPackage.GetGlobalService <SVsUIShellOpenDocument>() as IVsUIShellOpenDocument3; Guid reason = VSConstants.NewDocumentStateReason.Navigation; newDocumentStateContext = openDoc3.SetNewDocumentState((uint)__VSNEWDOCUMENTSTATE.NDS_Provisional, ref reason); EditorExtensionsPackage.DTE.ItemOperations.OpenFile(file); } finally { if (newDocumentStateContext != null) { newDocumentStateContext.Restore(); } } }
private bool HandleBlockComment(int position, string text, string indentation, int index) { int start = text.IndexOf("/*", StringComparison.Ordinal) + 2; int end = text.IndexOf("*/", StringComparison.Ordinal); if (start == 1 || end == -1 || index < start || index > end) { return(false); } string result = Environment.NewLine + indentation + " * " + Environment.NewLine + indentation + " "; using (EditorExtensionsPackage.UndoContext("Smart Indent")) { TextView.TextBuffer.Insert(position, result); SnapshotPoint point = new SnapshotPoint(TextView.TextBuffer.CurrentSnapshot, position + indentation.Length + 5); TextView.Caret.MoveTo(point); } return(true); }
protected override bool Execute(VSConstants.VSStd2KCmdID commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut) { int position = TextView.Caret.Position.BufferPosition.Position; SnapshotPoint point = new SnapshotPoint(TextView.TextBuffer.CurrentSnapshot, position); IWpfTextViewLine line = TextView.GetTextViewLineContainingBufferPosition(point); string text = TextView.TextBuffer.CurrentSnapshot.GetText(line.Start, line.Length); Match match = _indent.Match(text); if (match.Success) { using (EditorExtensionsPackage.UndoContext("Smart Indent")) { TextView.TextBuffer.Insert(position, Environment.NewLine + match.Value); } return(true); } return(false); }
public override void Invoke() { Sorter sorter = new Sorter(); if (_view.TextBuffer.ContentType.IsOfType("LESS")) { sorter.SortLess(_rule.Text); } else { sorter.SortStyleSheet(_rule.Text); } var position = _view.Caret.Position.BufferPosition.Position; EditorExtensionsPackage.DTE.UndoContext.Open(DisplayText); _view.Caret.MoveTo(new SnapshotPoint(_span.TextBuffer.CurrentSnapshot, position)); EditorExtensionsPackage.ExecuteCommand("Edit.FormatSelection"); EditorExtensionsPackage.DTE.UndoContext.Close(); }