Пример #1
0
 private void UpdateTextBuffer(SnapshotSpan span, string text)
 {
     using (WebEssentialsPackage.UndoContext("Comment/Uncomment"))
     {
         TextView.TextBuffer.Replace(span.Span, text);
     }
 }
Пример #2
0
        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)
            {
                WebEssentialsPackage.DTE.StatusBar.Text = "The selection is already minified";
                return(false);
            }

            using (WebEssentialsPackage.UndoContext("Minify"))
                TextView.TextBuffer.Replace(_spansTuple.Item1, result);

            return(true);
        }
Пример #3
0
        private bool Replace(Replacement callback)
        {
            TextDocument document    = GetTextDocument();
            string       replacement = callback(document.Selection.Text);

            using (WebEssentialsPackage.UndoContext((callback.Method.Name)))
                document.Selection.Insert(replacement, 0);

            return(true);
        }
        private bool HandleStartLines(int position, string indentation)
        {
            string result = Environment.NewLine + indentation + "* ";

            using (WebEssentialsPackage.UndoContext("Smart Indent"))
            {
                TextView.TextBuffer.Insert(position, result);
                SnapshotPoint point = new SnapshotPoint(TextView.TextBuffer.CurrentSnapshot, position + result.Length);
                TextView.Caret.MoveTo(point);
            }

            return(true);
        }
Пример #5
0
        private void Replace(Replacement callback)
        {
            TextDocument document = GetTextDocument();

            if (document == null)
            {
                return;
            }

            string replacement = callback(document.Selection.Text);

            using (WebEssentialsPackage.UndoContext((callback.Method.Name)))
                document.Selection.Insert(replacement, 0);
        }
Пример #6
0
 private static void UpdateBuffer(string innerHTML, HtmlEditorDocument html, Span span)
 {
     using (WebEssentialsPackage.UndoContext("Design Mode changes"))
     {
         try
         {
             html.TextBuffer.Replace(span, innerHTML);
             WebEssentialsPackage.DTE.ActiveDocument.Save();
         }
         catch
         {
             // Do nothing
         }
     }
 }
Пример #7
0
        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 (WebEssentialsPackage.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);
        }
Пример #8
0
        protected override bool Execute(LinesCommandId commandId, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            var span  = GetSpan();
            var lines = span.GetText().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);

            if (lines.Length == 0)
            {
                return(false);
            }

            string result = SortLines(commandId, lines);

            using (WebEssentialsPackage.UndoContext(("Sort Selected Lines")))
                TextView.TextBuffer.Replace(span.Span, result);

            return(true);
        }
Пример #9
0
        private static void AddMetaTag(int index)
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                var view   = ProjectHelpers.GetCurentTextView();
                var buffer = view.TextBuffer;

                using (WebEssentialsPackage.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);
                    WebEssentialsPackage.ExecuteCommand("Edit.FormatSelection");
                }

                WebEssentialsPackage.DTE.ActiveDocument.Save();
            }), DispatcherPriority.ApplicationIdle, null);
        }
Пример #10
0
        private static void AddMetaTag(int index)
        {
            Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
            {
                var view   = ProjectHelpers.GetCurentTextView();
                var buffer = view.TextBuffer;

                using (WebEssentialsPackage.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);
                    WebEssentialsPackage.ExecuteCommand("Edit.FormatSelection");
                }

                WebEssentialsPackage.DTE.ActiveDocument.Save();
                view.ViewScroller.EnsureSpanVisible(new SnapshotSpan(buffer.CurrentSnapshot, index, 1), EnsureSpanVisibleOptions.AlwaysCenter);
            }), DispatcherPriority.ApplicationIdle, null);
        }
Пример #11
0
        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"));
            bool         isString  = IsString(span);

            if (isComment || isString)
            {
                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 (WebEssentialsPackage.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);
        }
Пример #12
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 (WebEssentialsPackage.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);
        }
Пример #13
0
        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 (WebEssentialsPackage.UndoContext("Smart Indent"))
                {
                    TextView.TextBuffer.Insert(position, Environment.NewLine + match.Value);
                }

                return(true);
            }

            return(false);
        }