示例#1
0
 public EditorScript(IExportProvider exportProvider, CoreEditor coreEditor, IDisposable containerDisposable)
 {
     _exportProvider      = exportProvider;
     _coreEditor          = coreEditor;
     _editorShell         = _exportProvider.GetExportedValue <IEditorShell>();
     _containerDisposable = containerDisposable;
 }
示例#2
0
        public static void DoActionOnLines(ITextView textView, ITextBuffer textBuffer, ITextRange range, IEditorShell editorShell, Func<ITextSnapshotLine, bool> action, string actionName) {
            // When user clicks editor margin to select a line, selection actually
            // ends in the beginning of the next line. In order to prevent commenting
            // of the next line that user did not select, we need to shrink span to
            // format and exclude the trailing line break.

            ITextSnapshot snapshot = textBuffer.CurrentSnapshot;
            ITextSnapshotLine line = snapshot.GetLineFromPosition(range.End);

            int start = range.Start;
            int end = range.End;

            if (line.Start.Position == range.End && range.Length > 0) {
                if (line.LineNumber > 0) {
                    line = snapshot.GetLineFromLineNumber(line.LineNumber - 1);
                    end = line.End.Position;
                    start = Math.Min(start, end);
                }
            }

            int startLineNumber = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(start);
            int endLineNumber = textBuffer.CurrentSnapshot.GetLineNumberFromPosition(end);

            using (var undoAction = editorShell.CreateCompoundAction(textView, textBuffer)) {
                undoAction.Open(actionName);
                bool changed = false;
                for (int i = startLineNumber; i <= endLineNumber; i++) {
                    line = textBuffer.CurrentSnapshot.GetLineFromLineNumber(i);
                    changed |= action(line);
                }
                if (changed) {
                    undoAction.Commit();
                }
            }
        }
示例#3
0
 protected FunctionIndexBasedTest(AssemblyMefCatalogFixture catalog)
 {
     _exportProvider = catalog.CreateExportProvider();
     _editorShell    = _exportProvider.GetExportedValue <IEditorShell>();
     _packageIndex   = _exportProvider.GetExportedValue <IPackageIndex>();
     _functionIndex  = _exportProvider.GetExportedValue <IFunctionIndex>();
 }
示例#4
0
        public ROutliningTagger(REditorDocument document, IEditorShell shell)
            : base(document.EditorTree.TextBuffer, new ROutlineRegionBuilder(document, shell))
        {
            document.DocumentClosing += OnDocumentClosing;

            ServiceManager.AddService <ROutliningTagger>(this, document.EditorTree.TextBuffer, shell);
        }
示例#5
0
 public DocumentWindowBase(IEditorShell main, HierarchyNode item)
 {
     Shell           = main;
     Node            = item;
     Text            = item.NodeDataAdapter.GetDisplayName();
     item.Changed   += (s, a) => { Text = item.NodeDataAdapter.GetDisplayName(); };
     this.Activated += (s, a) => { Shell.Hierarchy.SetSelected(Node); };
 }
示例#6
0
        public static void HandleAutoformat(ITextView textView, IEditorShell editorShell, char typedChar) {
            if (!REditorSettings.AutoFormat) {
                return;
            }

            if (!REditorSettings.FormatScope && typedChar == '}') {
                return;
            }

            SnapshotPoint? rPoint = GetCaretPointInBuffer(textView);
            if (!rPoint.HasValue) {
                return;
            }

            var document = REditorDocument.FromTextBuffer(textView.TextBuffer);
            var ast = document.EditorTree.AstRoot;

            // Make sure we are not formatting damaging the projected range in R Markdown
            // which looks like ```{r. 'r' should not separate from {.
            var host = ContainedLanguageHost.GetHost(textView, document.TextBuffer, editorShell);
            if(host != null && !host.CanFormatLine(textView, document.TextBuffer, document.TextBuffer.CurrentSnapshot.GetLineNumberFromPosition(rPoint.Value))) {
                return;
            }

            // We don't want to auto-format inside strings
            if (ast.IsPositionInsideString(rPoint.Value.Position)) {
                return;
            }

            ITextBuffer subjectBuffer = rPoint.Value.Snapshot.TextBuffer;
            if (typedChar.IsLineBreak()) {
                // Special case for hitting caret after } and before 'else'. We do want to format
                // the construct as '} else {' but if user types Enter after } and we auto-format
                // it will look as if the editor just eats the Enter. Instead, we will not be
                // autoformatting in this specific case. User can always format either the document
                // or select the block and reformat it.
                if (!IsBetweenCurlyAndElse(subjectBuffer, rPoint.Value.Position)) {
                    var scopeStatement = GetFormatScope(textView, subjectBuffer, ast);
                    // Do not format large scope blocks for performance reasons
                    if (scopeStatement != null && scopeStatement.Length < 200) {
                        FormatOperations.FormatNode(textView, subjectBuffer, editorShell, scopeStatement);
                    } else {
                        FormatOperations.FormatViewLine(textView, subjectBuffer, -1, editorShell);
                    }
                }
            } else if (typedChar == ';') {
                // Verify we are at the end of the string and not in a middle
                // of another string or inside a statement.
                ITextSnapshotLine line = subjectBuffer.CurrentSnapshot.GetLineFromPosition(rPoint.Value.Position);
                int positionInLine = rPoint.Value.Position - line.Start;
                string lineText = line.GetText();
                if (positionInLine >= lineText.TrimEnd().Length) {
                    FormatOperations.FormatViewLine(textView, subjectBuffer, 0, editorShell);
                }
            } else if (typedChar == '}') {
                FormatOperations.FormatCurrentStatement(textView, subjectBuffer, editorShell, limitAtCaret: true, caretOffset: -1);
            }
        }
示例#7
0
        public EditorRoot(IEditorShell shell)
        {
            _editedModel = new Flow();

            var locationsFolder = this.AddFolder("Locations");

            locationsFolder.Commands.Add("Add directory", () =>
            {
                var location = new Location()
                {
                    Type = LocationType.Directory
                };
                _editedModel.Locations.Add(location);
            });

            locationsFolder.Commands.Add("Add file", () =>
            {
                var location = new Location()
                {
                    Type = LocationType.File
                };
                _editedModel.Locations.Add(location);
            });

            _editedModel.Locations.ItemAdded += (s, a) =>
            {
                var newNode = locationsFolder.Add(a.Item);
                newNode.Commands.Add("Remove", () => { _editedModel.Locations.Remove(a.Item); });

                var nameProp = newNode.AddProperty(x => x.Name);
                var typeProp = newNode.AddReadOnlyProperty(x => x.Type);

                string editorName = null;
                switch (a.Item.Type)
                {
                case LocationType.Directory:
                    editorName = typeof(System.Windows.Forms.Design.FolderNameEditor).AssemblyQualifiedName;
                    break;

                case LocationType.File:
                    editorName = typeof(System.Windows.Forms.Design.FileNameEditor).AssemblyQualifiedName;
                    break;
                }

                var pathProperty = newNode.AddProperty(x => x.Path);
                pathProperty.AssemblyQualifiedNameOfPropertyGridEditorType = editorName;


                newNode.Select();
            };

            _editedModel.Locations.ItemRemoved += (s, a) =>
            {
                var node = locationsFolder.Nodes().Where(x => x.Tag == a.Item).First();
                locationsFolder.Remove(node);
            };
        }
示例#8
0
 /// <summary>
 /// Formats specific AST node 
 /// </summary>
 public static void FormatNode(ITextView textView, ITextBuffer textBuffer, IEditorShell editorShell, IAstNode node, int limit = -1) {
     if (node != null) {
         if (limit >= 0 && limit < node.Start) {
             throw new ArgumentException(nameof(limit));
         }
         ITextRange range = limit < 0 ? node as ITextRange : TextRange.FromBounds(node.Start, limit);
         UndoableFormatRange(textView, textBuffer, range, editorShell);
     }
 }
示例#9
0
        public ROutlineRegionBuilder(IREditorDocument document, IEditorShell shell)
            : base(document.EditorTree.TextBuffer, shell) {
            EditorDocument = document;
            EditorDocument.DocumentClosing += OnDocumentClosing;

            EditorTree = document.EditorTree;
            EditorTree.UpdateCompleted += OnTreeUpdateCompleted;
            EditorTree.Closing += OnEditorTreeClosing;
        }
示例#10
0
        public CompoundUndoAction(ITextView textView, IEditorShell editorShell, bool addRollbackOnCancel = true) {
            if (!editorShell.IsUnitTestEnvironment) {
                IEditorOperationsFactoryService operationsService = editorShell.ExportProvider.GetExportedValue<IEditorOperationsFactoryService>();
                ITextBufferUndoManagerProvider undoProvider = editorShell.ExportProvider.GetExportedValue<ITextBufferUndoManagerProvider>();

                _editorOperations = operationsService.GetEditorOperations(textView);
                _undoManager = undoProvider.GetTextBufferUndoManager(_editorOperations.TextView.TextBuffer);
                _addRollbackOnCancel = addRollbackOnCancel;
            }
        }
示例#11
0
        protected OutlineRegionBuilder(ITextBuffer textBuffer, IEditorShell editorShell)
        {
            CurrentRegions = new OutlineRegionCollection(0);

            TextBuffer          = textBuffer;
            TextBuffer.Changed += OnTextBufferChanged;

            BackgroundTask = new IdleTimeAsyncTask(TaskAction, MainThreadAction, editorShell);
            BackgroundTask.DoTaskOnIdle(300);
        }
示例#12
0
        public ROutlineRegionBuilder(IREditorDocument document, IEditorShell shell)
            : base(document.EditorTree.TextBuffer, shell)
        {
            EditorDocument = document;
            EditorDocument.DocumentClosing += OnDocumentClosing;

            EditorTree = document.EditorTree;
            EditorTree.UpdateCompleted += OnTreeUpdateCompleted;
            EditorTree.Closing         += OnEditorTreeClosing;
        }
示例#13
0
        protected OutlineRegionBuilder(ITextBuffer textBuffer, IEditorShell editorShell) {
            CurrentRegions = new OutlineRegionCollection(0);

            TextBuffer = textBuffer;
            TextBuffer.Changed += OnTextBufferChanged;

            BackgroundTask = new IdleTimeAsyncTask(TaskAction, MainThreadAction, editorShell);
            if (IsEnabled) {
                BackgroundTask.DoTaskOnIdle(300);
            }
        }
示例#14
0
        /// <summary>
        /// Asynchronous idle time task constructor
        /// </summary>
        /// <param name="taskAction">Task to perform in a background thread</param>
        /// <param name="callbackAction">Callback to invoke when task completes</param>
        /// <param name="cancelAction">Callback to invoke if task is canceled</param>
        /// <param name="shell"></param>
        public IdleTimeAsyncTask(Func<object> taskAction, Action<object> callbackAction, Action<object> cancelAction, IEditorShell shell)
            : this(shell) {
            Debug.Assert(taskAction != null);

            if (taskAction == null)
                throw new ArgumentNullException(nameof(taskAction));

            _taskAction = taskAction;
            _callbackAction = callbackAction;
            _cancelAction = cancelAction;
        }
示例#15
0
        public CompoundUndoAction(ITextView textView, IEditorShell editorShell, bool addRollbackOnCancel = true)
        {
            if (!editorShell.IsUnitTestEnvironment)
            {
                IEditorOperationsFactoryService operationsService = editorShell.GlobalServices.GetService <IEditorOperationsFactoryService>();
                ITextBufferUndoManagerProvider  undoProvider      = editorShell.GlobalServices.GetService <ITextBufferUndoManagerProvider>();

                _editorOperations    = operationsService.GetEditorOperations(textView);
                _undoManager         = undoProvider.GetTextBufferUndoManager(_editorOperations.TextView.TextBuffer);
                _addRollbackOnCancel = addRollbackOnCancel;
            }
        }
示例#16
0
 /// <summary>
 /// Formats specific AST node
 /// </summary>
 public static void FormatNode(ITextView textView, ITextBuffer textBuffer, IEditorShell editorShell, IAstNode node, int limit = -1)
 {
     if (node != null)
     {
         if (limit >= 0 && limit < node.Start)
         {
             throw new ArgumentException(nameof(limit));
         }
         ITextRange range = limit < 0 ? node as ITextRange : TextRange.FromBounds(node.Start, limit);
         UndoableFormatRange(textView, textBuffer, range, editorShell);
     }
 }
示例#17
0
 public static OutlineRegionCollection BuildOutlineRegions(IEditorShell editorShell, string content) {
     TextBufferMock textBuffer = new TextBufferMock(content, RContentTypeDefinition.ContentType);
     using (var tree = new EditorTree(textBuffer, editorShell)) {
         tree.Build();
         using (var editorDocument = new EditorDocumentMock(tree)) {
             using (var ob = new ROutlineRegionBuilder(editorDocument, editorShell)) {
                 OutlineRegionCollection rc = new OutlineRegionCollection(0);
                 ob.BuildRegions(rc);
                 return rc;
             }
         }
     }
 }
示例#18
0
 /// <summary>
 /// Formats statement that the caret is at
 /// </summary>
 public static void FormatCurrentStatement(ITextView textView, ITextBuffer textBuffer, IEditorShell editorShell, bool limitAtCaret = false, int caretOffset = 0) {
     SnapshotPoint? caretPoint = REditorDocument.MapCaretPositionFromView(textView);
     if (!caretPoint.HasValue) {
         return;
     }
     IREditorDocument document = REditorDocument.TryFromTextBuffer(textBuffer);
     if (document != null) {
         ITextSnapshot snapshot = textBuffer.CurrentSnapshot;
         AstRoot ast = document.EditorTree.AstRoot;
         IAstNode node = ast.GetNodeOfTypeFromPosition<IStatement>(Math.Max(0, caretPoint.Value + caretOffset)) as IAstNode;
         FormatNode(textView, textBuffer, editorShell, node, limit: caretPoint.Value);
     }
 }
示例#19
0
        public IdleTimeAsyncTaskQueue(IEditorShell shell) {
            _shell = shell;
            var logicalCpuCount = Environment.ProcessorCount;

            var taskCount = logicalCpuCount / 4;
            if (taskCount == 0)
                taskCount = 1;

            _workerTasks = new IdleTimeAsyncTask[taskCount];

            for (int i = 0; i < _workerTasks.Length; i++) {
                _workerTasks[i] = new IdleTimeAsyncTask(_shell);
            }
        }
示例#20
0
 /// <summary>
 /// Incrementally applies whitespace change to the buffer
 /// having old and new tokens produced from the 'before formatting'
 /// and 'after formatting' versions of the same text.
 /// </summary>
 /// <param name="textBuffer">Text buffer to apply changes to</param>
 /// <param name="newTextProvider">Text provider of the text fragment before formatting</param>
 /// <param name="newTextProvider">Text provider of the formatted text</param>
 /// <param name="oldTokens">Tokens from the 'before' text fragment</param>
 /// <param name="newTokens">Tokens from the 'after' text fragment</param>
 /// <param name="formatRange">Range that is being formatted in the text buffer</param>
 /// <param name="transactionName">Name of the undo transaction to open</param>
 /// <param name="selectionTracker">Selection tracker object that will save, track
 /// <param name="additionalAction">Action to perform after changes are applies by undo unit is not yet closed.</param>
 /// and restore selection after changes have been applied.</param>
 public static void ApplyChangeByTokens(
     ITextBuffer textBuffer,
     ITextProvider oldTextProvider,
     ITextProvider newTextProvider,
     IReadOnlyList <ITextRange> oldTokens,
     IReadOnlyList <ITextRange> newTokens,
     ITextRange formatRange,
     string transactionName,
     ISelectionTracker selectionTracker,
     IEditorShell editorShell,
     Action additionalAction = null)
 {
     Debug.Assert(oldTokens.Count == newTokens.Count);
     if (oldTokens.Count == newTokens.Count)
     {
         ITextSnapshot snapshot = textBuffer.CurrentSnapshot;
         using (CreateSelectionUndo(selectionTracker, editorShell, transactionName)) {
             using (ITextEdit edit = textBuffer.CreateEdit()) {
                 if (oldTokens.Count > 0)
                 {
                     // Replace whitespace between tokens in reverse so relative positions match
                     int    oldEnd = oldTextProvider.Length;
                     int    newEnd = newTextProvider.Length;
                     string oldText, newText;
                     for (int i = newTokens.Count - 1; i >= 0; i--)
                     {
                         oldText = oldTextProvider.GetText(TextRange.FromBounds(oldTokens[i].End, oldEnd));
                         newText = newTextProvider.GetText(TextRange.FromBounds(newTokens[i].End, newEnd));
                         if (oldText != newText)
                         {
                             edit.Replace(formatRange.Start + oldTokens[i].End, oldEnd - oldTokens[i].End, newText);
                         }
                         oldEnd = oldTokens[i].Start;
                         newEnd = newTokens[i].Start;
                     }
                     newText = newTextProvider.GetText(TextRange.FromBounds(0, newEnd));
                     edit.Replace(formatRange.Start, oldEnd, newText);
                 }
                 else
                 {
                     string newText = newTextProvider.GetText(TextRange.FromBounds(0, newTextProvider.Length));
                     edit.Replace(formatRange.Start, formatRange.Length, newText);
                 }
                 edit.Apply();
                 additionalAction?.Invoke();
             }
         }
     }
 }
示例#21
0
        public static OutlineRegionCollection BuildOutlineRegions(IEditorShell editorShell, string content)
        {
            TextBufferMock textBuffer = new TextBufferMock(content, RContentTypeDefinition.ContentType);

            using (var tree = new EditorTree(textBuffer, editorShell)) {
                tree.Build();
                using (var editorDocument = new EditorDocumentMock(tree)) {
                    using (var ob = new ROutlineRegionBuilder(editorDocument, editorShell)) {
                        OutlineRegionCollection rc = new OutlineRegionCollection(0);
                        ob.BuildRegions(rc);
                        return(rc);
                    }
                }
            }
        }
示例#22
0
        public static void OutlineFile(IEditorShell editorShell, EditorTestFilesFixture fixture, string name) {
            string testFile = fixture.GetDestinationPath(name);
            string baselineFile = testFile + ".outline";
            string text = fixture.LoadDestinationFile(name);

            OutlineRegionCollection rc = BuildOutlineRegions(editorShell, text);
            string actual = TextRangeCollectionWriter.WriteCollection(rc);

            if (_regenerateBaselineFiles) {
                baselineFile = Path.Combine(fixture.SourcePath, Path.GetFileName(testFile)) + ".outline";
                TestFiles.UpdateBaseline(baselineFile, actual);
            } else {
                TestFiles.CompareToBaseLine(baselineFile, actual);
            }
        }
示例#23
0
        public OldEditorView(Editor editor, IEditorShell shell)
        {
            Verify.IsNotNull(editor, "editor");
            Verify.IsNotNull(shell, "shell");

            this.Shell  = shell;
            this.Editor = editor;
            this.Editor.ProgressChanged += new ProgressChangedEventHandler(Editor_ProgressChanged);

            SaveCommand  = new DelegateCommand(SaveProject, HasProject);
            CloseCommand = new DelegateCommand(CloseProject, HasProject);
            ExitCommand  = new DelegateCommand(Exit);
            HelpCommand  = new DelegateCommand(ShowHelp);

            UpdateSettings();
        }
示例#24
0
        protected override void OnStartup(StartupEventArgs e)
        {
            using (Editor = new Editor())
            {
                Editor.LoadExtensions();

                InitializeCulture(Editor);

                var splashWindow = GetSplashWindow(e.Args);
                var settings     = Editor.FindSettings <GeneralSettings>();

                var mainWindow = (Window)Editor.ToView();

                // Walk-around glass flash problems
                mainWindow.Left = SystemParameters.VirtualScreenWidth;
                mainWindow.Top  = SystemParameters.VirtualScreenHeight;

                mainWindow.SourceInitialized += (sender, args) =>
                {
                    if (settings.WindowPlacement != null)
                    {
                    }
                    mainWindow.WindowState = settings.WindowMaximized ? WindowState.Maximized : WindowState.Normal;
                    mainWindow.Width       = Math.Min(settings.WindowWidth, SystemParameters.VirtualScreenWidth);
                    mainWindow.Height      = Math.Min(settings.WindowHeight, SystemParameters.VirtualScreenHeight);
                    mainWindow.Left        = (SystemParameters.VirtualScreenWidth - mainWindow.Width) / 2;
                    mainWindow.Top         = (SystemParameters.VirtualScreenHeight - mainWindow.Height) / 2;

                    if (splashWindow != IntPtr.Zero)
                    {
                        ;// NativeMethods.ShowWindow(splashWindow, SW.HIDE);
                    }
                };

                mainWindow.Closed += (sender, args) =>
                {
                    settings.WindowPlacement = GetWindowPlacement(mainWindow);
                };

                Shell = (IEditorShell)mainWindow;
                Shell.ShowDialogAsync(new FilesView {
                    DataContext = App.Editor
                });

                mainWindow.Show();
            }
        }
示例#25
0
        public static bool FormatRange(ITextView textView, ITextBuffer textBuffer, ITextRange formatRange, RFormatOptions options, IEditorShell editorShell) {
            ITextSnapshot snapshot = textBuffer.CurrentSnapshot;
            int start = formatRange.Start;
            int end = formatRange.End;

            if(!CanFormatRange(textView, textBuffer, formatRange, editorShell)) {
                return false;
            }

            // When user clicks editor margin to select a line, selection actually
            // ends in the beginning of the next line. In order to prevent formatting
            // of the next line that user did not select, we need to shrink span to
            // format and exclude the trailing line break.
            ITextSnapshotLine line = snapshot.GetLineFromPosition(formatRange.End);

            if (line.Start.Position == formatRange.End && formatRange.Length > 0) {
                if (line.LineNumber > 0) {
                    line = snapshot.GetLineFromLineNumber(line.LineNumber - 1);
                    end = line.End.Position;
                    start = Math.Min(start, end);
                }
            }

            // Expand span to include the entire line
            ITextSnapshotLine startLine = snapshot.GetLineFromPosition(start);
            ITextSnapshotLine endLine = snapshot.GetLineFromPosition(end);

            // In case of formatting of multiline expressions formatter needs
            // to know the entire expression since otherwise it may not correctly
            // preserve user indentation. Consider 'x >% y' which is a plain statement
            // and needs to be indented at regular scope level vs
            //
            //      a %>% b %>%
            //          x %>% y
            //
            // where user indentation of 'x %>% y' must be preserved. We don't have
            // complete information here since expression may not be syntactically
            // correct and hence AST may not have correct information and besides,
            // the AST is damaged at this point. As a workaround, we will check 
            // if the previous line ends with an operator current line starts with 
            // an operator.
            int startPosition = FindStartOfExpression(textBuffer, startLine.Start);

            formatRange = TextRange.FromBounds(startPosition, endLine.End);
            return FormatRangeExact(textView, textBuffer, formatRange, options, editorShell);
        }
示例#26
0
        public static void OutlineFile(IEditorShell editorShell, EditorTestFilesFixture fixture, string name)
        {
            string testFile     = fixture.GetDestinationPath(name);
            string baselineFile = testFile + ".outline";
            string text         = fixture.LoadDestinationFile(name);

            OutlineRegionCollection rc = BuildOutlineRegions(editorShell, text);
            string actual = TextRangeCollectionWriter.WriteCollection(rc);

            if (_regenerateBaselineFiles)
            {
                baselineFile = Path.Combine(fixture.SourcePath, Path.GetFileName(testFile)) + ".outline";
                TestFiles.UpdateBaseline(baselineFile, actual);
            }
            else
            {
                TestFiles.CompareToBaseLine(baselineFile, actual);
            }
        }
        /// <summary>
        /// Incrementally applies whitespace change to the buffer 
        /// having old and new tokens produced from the 'before formatting' 
        /// and 'after formatting' versions of the same text.
        /// </summary>
        /// <param name="textBuffer">Text buffer to apply changes to</param>
        /// <param name="newTextProvider">Text provider of the text fragment before formatting</param>
        /// <param name="newTextProvider">Text provider of the formatted text</param>
        /// <param name="oldTokens">Tokens from the 'before' text fragment</param>
        /// <param name="newTokens">Tokens from the 'after' text fragment</param>
        /// <param name="formatRange">Range that is being formatted in the text buffer</param>
        /// <param name="transactionName">Name of the undo transaction to open</param>
        /// <param name="selectionTracker">Selection tracker object that will save, track
        /// <param name="additionalAction">Action to perform after changes are applies by undo unit is not yet closed.</param>
        /// and restore selection after changes have been applied.</param>
        public static void ApplyChangeByTokens(
            ITextBuffer textBuffer,
            ITextProvider oldTextProvider,
            ITextProvider newTextProvider,
            IReadOnlyList<ITextRange> oldTokens,
            IReadOnlyList<ITextRange> newTokens,
            ITextRange formatRange,
            string transactionName,
            ISelectionTracker selectionTracker,
            IEditorShell editorShell,
            Action additionalAction = null) {

            Debug.Assert(oldTokens.Count == newTokens.Count);
            if (oldTokens.Count == newTokens.Count) {
                ITextSnapshot snapshot = textBuffer.CurrentSnapshot;
                using (CreateSelectionUndo(selectionTracker, editorShell, transactionName)) {
                    using (ITextEdit edit = textBuffer.CreateEdit()) {
                        if (oldTokens.Count > 0) {
                            // Replace whitespace between tokens in reverse so relative positions match
                            int oldEnd = oldTextProvider.Length;
                            int newEnd = newTextProvider.Length;
                            string oldText, newText;
                            for (int i = newTokens.Count - 1; i >= 0; i--) {
                                oldText = oldTextProvider.GetText(TextRange.FromBounds(oldTokens[i].End, oldEnd));
                                newText = newTextProvider.GetText(TextRange.FromBounds(newTokens[i].End, newEnd));
                                if (oldText != newText) {
                                    edit.Replace(formatRange.Start + oldTokens[i].End, oldEnd - oldTokens[i].End, newText);
                                }
                                oldEnd = oldTokens[i].Start;
                                newEnd = newTokens[i].Start;
                            }
                            newText = newTextProvider.GetText(TextRange.FromBounds(0, newEnd));
                            edit.Replace(formatRange.Start, oldEnd, newText);
                        } else {
                            string newText = newTextProvider.GetText(TextRange.FromBounds(0, newTextProvider.Length));
                            edit.Replace(formatRange.Start, formatRange.Length, newText);
                        }
                        edit.Apply();
                        additionalAction?.Invoke();
                    }
                }
            }
        }
示例#28
0
        public IdleTimeAsyncTaskQueue(IEditorShell shell)
        {
            _shell = shell;
            var logicalCpuCount = Environment.ProcessorCount;

            var taskCount = logicalCpuCount / 4;

            if (taskCount == 0)
            {
                taskCount = 1;
            }

            _workerTasks = new IdleTimeAsyncTask[taskCount];

            for (int i = 0; i < _workerTasks.Length; i++)
            {
                _workerTasks[i] = new IdleTimeAsyncTask(_shell);
            }
        }
示例#29
0
        public static bool FormatRangeExact(ITextView textView, ITextBuffer textBuffer, ITextRange formatRange, RFormatOptions options, IEditorShell editorShell) {
            ITextSnapshot snapshot = textBuffer.CurrentSnapshot;
            Span spanToFormat = new Span(formatRange.Start, formatRange.Length);
            string spanText = snapshot.GetText(spanToFormat.Start, spanToFormat.Length);
            string trimmedSpanText = spanText.Trim();

            RFormatter formatter = new RFormatter(options);
            string formattedText = formatter.Format(trimmedSpanText);

            formattedText = formattedText.Trim(); // There may be inserted line breaks after {
            // Apply formatted text without indentation. We then will update the parse tree 
            // so we can calculate proper line indents from the AST via the smart indenter.
            if (!spanText.Equals(formattedText, StringComparison.Ordinal)) {
                // Extract existing indent before applying changes. Existing indent
                // may be used by the smart indenter for function argument lists.
                var startLine = snapshot.GetLineFromPosition(spanToFormat.Start);
                var originalIndentSizeInSpaces = IndentBuilder.TextIndentInSpaces(startLine.GetText(), options.IndentSize);

                var selectionTracker = new RSelectionTracker(textView, textBuffer, formatRange);
                RTokenizer tokenizer = new RTokenizer();
                IReadOnlyTextRangeCollection<RToken> oldTokens = tokenizer.Tokenize(spanText);
                IReadOnlyTextRangeCollection<RToken> newTokens = tokenizer.Tokenize(formattedText);

                IncrementalTextChangeApplication.ApplyChangeByTokens(
                    textBuffer,
                    new TextStream(spanText), new TextStream(formattedText),
                    oldTokens, newTokens,
                    formatRange,
                    Resources.AutoFormat, selectionTracker, editorShell,
                    () => {
                        var ast = UpdateAst(textBuffer);
                        // Apply indentation
                        IndentLines(textView, textBuffer, new TextRange(formatRange.Start, formattedText.Length), ast, options, originalIndentSizeInSpaces);
                    });

                return true;
            }

            return false;
        }
示例#30
0
        public static void FormatCurrentScope(ITextView textView, ITextBuffer textBuffer, IEditorShell editorShell, bool indentCaret) {
            // Figure out caret position in the document text buffer
            SnapshotPoint? caretPoint = REditorDocument.MapCaretPositionFromView(textView);
            if (!caretPoint.HasValue) {
                return;
            }
            IREditorDocument document = REditorDocument.TryFromTextBuffer(textBuffer);
            if (document != null) {
                // Make sure AST is up to date
                document.EditorTree.EnsureTreeReady();
                var ast = document.EditorTree.AstRoot;
                ITextSnapshot snapshot = textBuffer.CurrentSnapshot;

                // Find scope to format
                IScope scope = ast.GetNodeOfTypeFromPosition<IScope>(caretPoint.Value);

                using (var undoAction = editorShell.CreateCompoundAction(textView, textView.TextBuffer)) {
                    undoAction.Open(Resources.AutoFormat);
                    // Now format the scope
                    bool changed = RangeFormatter.FormatRange(textView, textBuffer, scope, REditorSettings.FormatOptions, editorShell);
                    if (indentCaret) {
                        // Formatting may change AST and the caret position so we need to reacquire both
                        caretPoint = REditorDocument.MapCaretPositionFromView(textView);
                        if (caretPoint.HasValue) {
                            document.EditorTree.EnsureTreeReady();
                            ast = document.EditorTree.AstRoot;
                            scope = ast.GetNodeOfTypeFromPosition<IScope>(caretPoint.Value);
                            IndentCaretInNewScope(textView, scope, caretPoint.Value, REditorSettings.FormatOptions);
                        }
                    }
                    if (changed) {
                        undoAction.Commit();
                    }
                }
            }
        }
示例#31
0
 public ReplFormatDocumentCommand(ITextView view, ITextBuffer buffer, IEditorShell editorShell)
     : base(view, buffer, editorShell)
 {
 }
示例#32
0
        public ROutliningTagger(REditorDocument document, IEditorShell shell)
            : base(document.EditorTree.TextBuffer, new ROutlineRegionBuilder(document, shell)) {
            document.DocumentClosing += OnDocumentClosing;

            ServiceManager.AddService<ROutliningTagger>(this, document.EditorTree.TextBuffer, shell);
        }
示例#33
0
        public static void UndoableFormatRange(ITextView textView, ITextBuffer textBuffer, ITextRange formatRange, IEditorShell editorShell, bool exactRange = false) {
            using (var undoAction = editorShell.CreateCompoundAction(textView, textView.TextBuffer)) {
                undoAction.Open(Resources.AutoFormat);
                var result = exactRange
                    ? RangeFormatter.FormatRangeExact(textView, textBuffer, formatRange, REditorSettings.FormatOptions, editorShell)
                    : RangeFormatter.FormatRange(textView, textBuffer, formatRange, REditorSettings.FormatOptions, editorShell);

                if (result) {
                    undoAction.Commit();
                }
            }
        }
示例#34
0
 public FormatCommandTest(IExportProvider exportProvider)
 {
     _editorShell = exportProvider.GetExportedValue <IEditorShell>();
 }
示例#35
0
 public ROutlineBuilderTest(REditorMefCatalogFixture catalogFixture, EditorTestFilesFixture testFiles)
 {
     _exportProvider = catalogFixture.CreateExportProvider();
     _editorShell    = _exportProvider.GetExportedValue <IEditorShell>();
     _testFiles      = testFiles;
 }
示例#36
0
        public static void FormatCurrentScope(ITextView textView, ITextBuffer textBuffer, IEditorShell editorShell, bool indentCaret)
        {
            // Figure out caret position in the document text buffer
            SnapshotPoint?caretPoint = REditorDocument.MapCaretPositionFromView(textView);

            if (!caretPoint.HasValue)
            {
                return;
            }
            IREditorDocument document = REditorDocument.TryFromTextBuffer(textBuffer);

            if (document != null)
            {
                // Make sure AST is up to date
                document.EditorTree.EnsureTreeReady();
                var           ast      = document.EditorTree.AstRoot;
                ITextSnapshot snapshot = textBuffer.CurrentSnapshot;

                // Find scope to format
                IScope scope = ast.GetNodeOfTypeFromPosition <IScope>(caretPoint.Value);

                using (var undoAction = editorShell.CreateCompoundAction(textView, textView.TextBuffer)) {
                    undoAction.Open(Resources.AutoFormat);
                    // Now format the scope
                    bool changed = RangeFormatter.FormatRange(textView, textBuffer, scope, REditorSettings.FormatOptions, editorShell);
                    if (indentCaret)
                    {
                        // Formatting may change AST and the caret position so we need to reacquire both
                        caretPoint = REditorDocument.MapCaretPositionFromView(textView);
                        if (caretPoint.HasValue)
                        {
                            document.EditorTree.EnsureTreeReady();
                            ast   = document.EditorTree.AstRoot;
                            scope = ast.GetNodeOfTypeFromPosition <IScope>(caretPoint.Value);
                            IndentCaretInNewScope(textView, scope, caretPoint.Value, REditorSettings.FormatOptions);
                        }
                    }
                    if (changed)
                    {
                        undoAction.Commit();
                    }
                }
            }
        }
示例#37
0
        /// <summary>
        /// Formats statement that the caret is at
        /// </summary>
        public static void FormatCurrentStatement(ITextView textView, ITextBuffer textBuffer, IEditorShell editorShell, bool limitAtCaret = false, int caretOffset = 0)
        {
            SnapshotPoint?caretPoint = REditorDocument.MapCaretPositionFromView(textView);

            if (!caretPoint.HasValue)
            {
                return;
            }
            IREditorDocument document = REditorDocument.TryFromTextBuffer(textBuffer);

            if (document != null)
            {
                ITextSnapshot snapshot = textBuffer.CurrentSnapshot;
                AstRoot       ast      = document.EditorTree.AstRoot;
                IAstNode      node     = ast.GetNodeOfTypeFromPosition <IStatement>(Math.Max(0, caretPoint.Value + caretOffset)) as IAstNode;
                FormatNode(textView, textBuffer, editorShell, node, limit: caretPoint.Value);
            }
        }
示例#38
0
        /// <summary>
        /// Formats line the caret is currently at
        /// </summary>
        public static void FormatViewLine(ITextView textView, ITextBuffer textBuffer, int offset, IEditorShell editorShell)
        {
            SnapshotPoint?caretPoint = REditorDocument.MapCaretPositionFromView(textView);

            if (!caretPoint.HasValue)
            {
                return;
            }

            ITextSnapshot     snapshot    = textBuffer.CurrentSnapshot;
            int               lineNumber  = snapshot.GetLineNumberFromPosition(caretPoint.Value.Position);
            ITextSnapshotLine line        = snapshot.GetLineFromLineNumber(lineNumber + offset);
            ITextRange        formatRange = new TextRange(line.Start, line.Length);

            UndoableFormatRange(textView, textBuffer, formatRange, editorShell, exactRange: true);
        }
        private static IDisposable CreateSelectionUndo(ISelectionTracker selectionTracker, IEditorShell editorShell, string transactionName) {
            if (editorShell.IsUnitTestEnvironment) {
                return Disposable.Empty;
            }

            var textBufferUndoManagerProvider = editorShell.ExportProvider.GetExportedValue<ITextBufferUndoManagerProvider>();
            return new SelectionUndo(selectionTracker, textBufferUndoManagerProvider, transactionName, automaticTracking: false);
        }
示例#40
0
 public ROutlineBuilderTest(REditorMefCatalogFixture catalogFixture, EditorTestFilesFixture testFiles) {
     _exportProvider = catalogFixture.CreateExportProvider();
     _editorShell = _exportProvider.GetExportedValue<IEditorShell>();
     _testFiles = testFiles;
 }
示例#41
0
 public GotoBraceCommandTest(IExportProvider exportProvider)
 {
     _editorShell = exportProvider.GetExportedValue <IEditorShell>();
 }
示例#42
0
 public RCommandFactory([Import(AllowDefault = true)] IObjectViewer objectViewer, [Import(AllowDefault = true)] IRInteractiveWorkflowProvider workflowProvider, IEditorShell editorShell)
 {
     _objectViewer     = objectViewer;
     _workflowProvider = workflowProvider;
     _editorShell      = editorShell;
 }
示例#43
0
        /// <summary>
        /// Formats line the caret is currently at
        /// </summary>
        public static void FormatViewLine(ITextView textView, ITextBuffer textBuffer, int offset, IEditorShell editorShell) {
            SnapshotPoint? caretPoint = REditorDocument.MapCaretPositionFromView(textView);
            if (!caretPoint.HasValue) {
                return;
            }

            ITextSnapshot snapshot = textBuffer.CurrentSnapshot;
            int lineNumber = snapshot.GetLineNumberFromPosition(caretPoint.Value.Position);
            ITextSnapshotLine line = snapshot.GetLineFromLineNumber(lineNumber + offset);
            ITextRange formatRange = new TextRange(line.Start, line.Length);

            UndoableFormatRange(textView, textBuffer, formatRange, editorShell, exactRange: true);
        }
示例#44
0
 public EditingCommand(ITextView textView, IEditorShell editorShell, CommandId id)
     : base(textView, id, true)
 {
     EditorShell = editorShell;
 }
示例#45
0
        public static void UndoableFormatRange(ITextView textView, ITextBuffer textBuffer, ITextRange formatRange, IEditorShell editorShell, bool exactRange = false)
        {
            using (var undoAction = editorShell.CreateCompoundAction(textView, textView.TextBuffer)) {
                undoAction.Open(Resources.AutoFormat);
                var result = exactRange
                    ? RangeFormatter.FormatRangeExact(textView, textBuffer, formatRange, REditorSettings.FormatOptions, editorShell)
                    : RangeFormatter.FormatRange(textView, textBuffer, formatRange, REditorSettings.FormatOptions, editorShell);

                if (result)
                {
                    undoAction.Commit();
                }
            }
        }
示例#46
0
 public ReplFormatDocumentCommand(ITextView view, ITextBuffer buffer, IEditorShell editorShell) 
     : base(view, buffer, editorShell) { }
示例#47
0
 public CommenterTest(REditorMefCatalogFixture catalogFixture) {
     _exportProvider = catalogFixture.CreateExportProvider();
     _editorShell = _exportProvider.GetExportedValue<IEditorShell>();
 }
示例#48
0
        private static bool CanFormatRange(ITextView textView, ITextBuffer textBuffer, ITextRange formatRange, IEditorShell editorShell) {
            // Make sure we are not formatting damaging the projected range in R Markdown
            // which looks like ```{r. 'r' should not separate from {.
            var host = ContainedLanguageHost.GetHost(textView, textBuffer, editorShell);
            if (host != null) {
                ITextSnapshot snapshot = textBuffer.CurrentSnapshot;

                int startLine = snapshot.GetLineNumberFromPosition(formatRange.Start);
                int endLine = snapshot.GetLineNumberFromPosition(formatRange.End);
                for(int i = startLine; i<= endLine; i++) {
                    if (!host.CanFormatLine(textView, textBuffer, i)) {
                        return false;
                    }
                }
            }
            return true;
        }
示例#49
0
 public Hierarchy CreateHierarchy(IEditorShell shell)
 {
     return(new TerminalConfigurationHierarchy(shell));
 }
示例#50
0
 /// <summary>
 /// Asynchronous idle time task constructor
 /// </summary>
 /// <param name="taskAction">Task to perform in a background thread</param>
 /// <param name="shell"></param>
 public IdleTimeAsyncTask(Func<object> taskAction, IEditorShell shell)
     : this(taskAction, null, null, shell) {
 }
示例#51
0
 public TerminalConfigurationHierarchy(IEditorShell shell)
 {
     this.shell = shell;
     BindRoot();
 }
示例#52
0
 public GotoBraceCommandTest(REditorMefCatalogFixture catalog) {
     _exportProvider = catalog.CreateExportProvider();
     _editorShell = _exportProvider.GetExportedValue<IEditorShell>();
 }
示例#53
0
 public IdleTimeAsyncTask(IEditorShell shell) {
     _shell = shell;
 }
 public BraceCompletionContextProvider(IEditorShell shell) {
     _shell = shell;
 }
示例#55
0
        public static void HandleAutoformat(ITextView textView, IEditorShell editorShell, char typedChar)
        {
            if (!REditorSettings.AutoFormat)
            {
                return;
            }

            if (!REditorSettings.FormatScope && typedChar == '}')
            {
                return;
            }

            SnapshotPoint?rPoint = GetCaretPointInBuffer(textView);

            if (!rPoint.HasValue)
            {
                return;
            }

            var document = REditorDocument.FromTextBuffer(textView.TextBuffer);
            var ast      = document.EditorTree.AstRoot;

            // Make sure we are not formatting damaging the projected range in R Markdown
            // which looks like ```{r. 'r' should not separate from {.
            var host = ContainedLanguageHost.GetHost(textView, document.TextBuffer, editorShell);

            if (host != null && !host.CanFormatLine(textView, document.TextBuffer, document.TextBuffer.CurrentSnapshot.GetLineNumberFromPosition(rPoint.Value)))
            {
                return;
            }

            // We don't want to auto-format inside strings
            if (ast.IsPositionInsideString(rPoint.Value.Position))
            {
                return;
            }

            ITextBuffer subjectBuffer = rPoint.Value.Snapshot.TextBuffer;

            if (typedChar.IsLineBreak())
            {
                // Special case for hitting caret after } and before 'else'. We do want to format
                // the construct as '} else {' but if user types Enter after } and we auto-format
                // it will look as if the editor just eats the Enter. Instead, we will not be
                // autoformatting in this specific case. User can always format either the document
                // or select the block and reformat it.
                if (!IsBetweenCurlyAndElse(subjectBuffer, rPoint.Value.Position))
                {
                    var scopeStatement = GetFormatScope(textView, subjectBuffer, ast);
                    // Do not format large scope blocks for performance reasons
                    if (scopeStatement != null && scopeStatement.Length < 200)
                    {
                        FormatOperations.FormatNode(textView, subjectBuffer, editorShell, scopeStatement);
                    }
                    else if (CanFormatLine(textView, subjectBuffer, -1))
                    {
                        FormatOperations.FormatViewLine(textView, subjectBuffer, -1, editorShell);
                    }
                }
            }
            else if (typedChar == ';')
            {
                // Verify we are at the end of the string and not in a middle
                // of another string or inside a statement.
                ITextSnapshotLine line = subjectBuffer.CurrentSnapshot.GetLineFromPosition(rPoint.Value.Position);
                int    positionInLine  = rPoint.Value.Position - line.Start;
                string lineText        = line.GetText();
                if (positionInLine >= lineText.TrimEnd().Length)
                {
                    FormatOperations.FormatViewLine(textView, subjectBuffer, 0, editorShell);
                }
            }
            else if (typedChar == '}')
            {
                FormatOperations.FormatCurrentStatement(textView, subjectBuffer, editorShell, limitAtCaret: true, caretOffset: -1);
            }
        }
示例#56
0
 internal UncommentCommand(ITextView textView, ITextBuffer textBuffer, IEditorShell editorShell)
     : base(textView, editorShell, new CommandId(VSConstants.VSStd2K, (int)VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK)) {
 }
示例#57
0
 public RangeFormatterTest(IExportProvider exportProvider) {
     _exportProvider = exportProvider;
     _editorShell = _exportProvider.GetExportedValue<IEditorShell>();
 }
示例#58
0
 public DropHandlerProvider(IEditorShell editorShell, IRInteractiveWorkflowProvider workflowProvider) {
     _editorShell = editorShell;
     _workflowProvider = workflowProvider;
 }
示例#59
0
 public DropHandlerProvider(IEditorShell editorShell, IWorkspaceServices wsps)
 {
     _editorShell = editorShell;
     _wsps        = wsps;
 }
示例#60
0
 public EditingCommand(ITextView textView, IEditorShell editorShell, CommandId id)
     : base(textView, id, true) {
     EditorShell = editorShell;
 }