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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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 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);
        }
예제 #5
0
        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);
        }
        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
                }
            }
        }
예제 #8
0
        protected override bool Execute(MinifyCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (span == null)
            {
                return(false);
            }
            var    source = span.Value.GetText();
            string result = Mef.GetImport <IFileMinifier>(span.Value.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(span.Value.Span, result);

            return(true);
        }