Ejemplo n.º 1
0
        /// <summary>
        /// Sets the cursor to the first position of the given line.
        /// </summary>
        /// <param name="codePane"></param>
        /// <param name="lineNumber"></param>
        public static void SetSelection(this CodePane codePane, int lineNumber)
        {
            var line      = codePane.CodeModule.Lines[lineNumber, 1];
            var selection = new Selection(lineNumber, 1, lineNumber, line.Length);

            codePane.SetSelection(selection);
        }
Ejemplo n.º 2
0
    public static Selection?GetSelection(this CodePane pane)
    {
        if (pane == null)
        {
            return(null);
        }

        int startLine;
        int endLine;
        int startColumn;
        int endColumn;

        pane.GetSelection(out startLine, out startColumn, out endLine, out endColumn);

        if (endLine > startLine && endColumn == 1)
        {
            endLine--;
            endColumn = pane.CodeModule.Lines[endLine, 1].Length;
        }

        if (startLine == 0 ||
            startColumn == 0 ||
            endLine == 0 ||
            endColumn == 0)
        {
            return(new Selection?());
        }

        return(new Selection(startLine, startColumn, endLine, endColumn));
    }
Ejemplo n.º 3
0
        public static QualifiedSelection GetSelection(this CodePane pane)
        {
            int startLine;
            int endLine;
            int startColumn;
            int endColumn;

            if (pane == null)
            {
                return(new QualifiedSelection());
            }

            pane.GetSelection(out startLine, out startColumn, out endLine, out endColumn);

            if (endLine > startLine && endColumn == 1)
            {
                endLine--;
                endColumn = pane.CodeModule.get_Lines(endLine, 1).Length;
            }

            var selection  = new Selection(startLine, startColumn, endLine, endColumn);
            var moduleName = new QualifiedModuleName(pane.CodeModule.Parent);

            return(new QualifiedSelection(moduleName, selection));
        }
Ejemplo n.º 4
0
        /// <summary>   A CodePane extension method that selected procedure. </summary>
        ///
        /// <param name="selection">    The selection. </param>
        /// <returns>   A Selection object representing the procedure the cursor is currently in. </returns>
        public static Selection SelectedProcedure(this CodePane code, Selection selection)
        {
            vbext_ProcKind kind;
            var            procedure = code.CodeModule.get_ProcOfLine(selection.StartLine, out kind);
            var            startLine = code.CodeModule.get_ProcStartLine(procedure, kind);
            var            endLine   = startLine + code.CodeModule.get_ProcCountLines(procedure, kind) + 1;

            return(new Selection(startLine, 1, endLine, 1));
        }
        public Declaration FindSelectedDeclaration(CodePane activeCodePane, bool procedureLevelOnly = false)
        {
            var selection = activeCodePane.GetSelection();

            if (selection.Equals(_lastSelection))
            {
                return(_selectedDeclaration);
            }

            _lastSelection       = selection;
            _selectedDeclaration = null;

            if (!selection.Equals(default(QualifiedSelection)))
            {
                var matches = AllDeclarations
                              .Where(item => item.DeclarationType != DeclarationType.Project &&
                                     item.DeclarationType != DeclarationType.ModuleOption &&
                                     item.DeclarationType != DeclarationType.ClassModule &&
                                     item.DeclarationType != DeclarationType.ProceduralModule &&
                                     (IsSelectedDeclaration(selection, item) ||
                                      item.References.Any(reference => IsSelectedReference(selection, reference))))
                              .ToList();
                try
                {
                    if (matches.Count == 1)
                    {
                        _selectedDeclaration = matches.Single();
                    }
                    else
                    {
                        Declaration match = null;
                        if (procedureLevelOnly)
                        {
                            match = matches.SingleOrDefault(item => item.DeclarationType.HasFlag(DeclarationType.Member));
                        }

                        // ambiguous (?), or no match - make the module be the current selection
                        match = match ?? AllUserDeclarations.SingleOrDefault(item =>
                                                                             (item.DeclarationType == DeclarationType.ClassModule || item.DeclarationType == DeclarationType.ProceduralModule) &&
                                                                             item.QualifiedName.QualifiedModuleName.Equals(selection.QualifiedName));

                        _selectedDeclaration = match;
                    }
                }
                catch (InvalidOperationException exception)
                {
                    Debug.WriteLine(exception);
                }
            }

            if (_selectedDeclaration != null)
            {
                Debug.WriteLine("Current selection ({0}) is '{1}' ({2})", selection, _selectedDeclaration.IdentifierName, _selectedDeclaration.DeclarationType);
            }

            return(_selectedDeclaration);
        }
Ejemplo n.º 6
0
        public ChangeContext(CodePane codePane, Document document)
        {
            Debug.Assert(document.stringBuilder.ToString() == document.currentText);

            this.codePane = codePane;
            this.document = document;
            document.stringBuilder.SuspendChangedEvents();
            document.undoRedoHistory.Track(document.stringBuilder, document.Caret, codePane.Selection);
        }
Ejemplo n.º 7
0
 public CompletionPane(
     CodePane codePane,
     IPromptCallbacks promptCallbacks,
     PromptConfiguration configuration)
 {
     this.codePane        = codePane;
     this.promptCallbacks = promptCallbacks;
     this.configuration   = configuration;
 }
        public static void AddComponent(this IVBProject vbProject, string name, ComponentType type, string content, Selection selection = new Selection())
        {
            var component = new VbComponent(vbProject.VBE, name, type, vbProject.VBComponents);
            var codePane  = new CodePane(vbProject.VBE, new Window(name), selection, component);

            vbProject.VBE.ActiveCodePane = codePane;
            component.CodeModule         = codePane.CodeModule = new CodeModule(vbProject.VBE, name, content, component, codePane);
            ((VbComponents)vbProject.VBComponents).Add(component);
        }
Ejemplo n.º 9
0
    private async Task InsertCompletion(CodePane codepane, CompletionItem completion, CancellationToken cancellationToken)
    {
        var document      = codepane.Document;
        var spanToReplace = await promptCallbacks.GetSpanToReplaceByCompletionkAsync(document.GetText(), document.Caret, cancellationToken).ConfigureAwait(false);

        document.Remove(codepane, spanToReplace);
        document.InsertAtCaret(codepane, completion.ReplacementText);
        document.Caret = spanToReplace.Start + completion.ReplacementText.Length;
        Close();
    }
Ejemplo n.º 10
0
        /// <summary>   A CodePane extension method that gets the current selection. </summary>
        /// <returns>   The selection. </returns>
        public static Selection GetSelection(this CodePane code)
        {
            int startLine;
            int endLine;
            int startColumn;
            int endColumn;

            code.GetSelection(out startLine, out startColumn, out endLine, out endColumn);
            return(new Selection(startLine, startColumn, endLine, endColumn));
        }
Ejemplo n.º 11
0
        private static Selection GetSelection(CodePane codePane)
        {
            int startLine;
            int startColumn;
            int endLine;
            int endColumn;

            codePane.GetSelection(out startLine, out startColumn, out endLine, out endColumn);
            return(new Selection(startLine, startColumn, endLine, endColumn));
        }
Ejemplo n.º 12
0
    private async Task <PromptResult?> GetResult(CodePane codePane, KeyPress key, string inputText, CancellationToken cancellationToken)
    {
        // process any user-defined keyboard shortcuts
        if (promptCallbacks.TryGetKeyPressCallbacks(key.ConsoleKeyInfo, out var callback))
        {
            var result = await callback.Invoke(inputText, codePane.Document.Caret, cancellationToken).ConfigureAwait(false);

            if (result is not null)
            {
                return(result);
            }
        }
        return(codePane.Result);
    }
Ejemplo n.º 13
0
    /// <inheritdoc cref="IPrompt.ReadLineAsync()" />
    public async Task <PromptResult> ReadLineAsync()
    {
        var renderer = new Renderer(console, configuration);

        renderer.RenderPrompt();

        // code pane contains the code the user is typing. It does not include the prompt (i.e. "> ")
        var codePane = new CodePane(console, configuration, clipboard);

        // completion pane is the pop-up window that shows potential autocompletions.
        var completionPane = new CompletionPane(
            codePane,
            promptCallbacks,
            configuration);

        codePane.Bind(completionPane);

        history.Track(codePane);
        cancellationManager.CaptureControlC();

        foreach (var key in KeyPress.ReadForever(console))
        {
            // grab the code area width every key press, so we rerender appropriately when the console is resized.
            codePane.MeasureConsole();

            await InterpretKeyPress(key, codePane, completionPane, cancellationToken : default).ConfigureAwait(false);

            // typing / word-wrapping may have scrolled the console, giving us more room.
            codePane.MeasureConsole();

            // render the typed input, with syntax highlighting
            var inputText  = codePane.Document.GetText();
            var highlights = await highlighter.HighlightAsync(inputText, cancellationToken : default).ConfigureAwait(false);

            // the key press may have caused the prompt to return its input, e.g. <Enter> or a callback.
            var result = await GetResult(codePane, key, inputText, cancellationToken : default).ConfigureAwait(false);

            await renderer.RenderOutput(result, codePane, completionPane, highlights, key, cancellationToken : default).ConfigureAwait(false);

            if (result is not null)
            {
                _ = history.SavePersistentHistoryAsync(inputText);
                cancellationManager.AllowControlCToCancelResult(result);
                return(result);
            }
        }

        Debug.Assert(false, "Should never reach here due to infinite " + nameof(KeyPress.ReadForever));
        return(null);
    }
Ejemplo n.º 14
0
        public static void ForceFocus(this CodePane pane)
        {
            pane.Show();

            var mainWindowHandle  = pane.VBE.MainWindow.Handle();
            var childWindowFinder = new NativeMethods.ChildWindowFinder(pane.Window.Caption);

            NativeMethods.EnumChildWindows(mainWindowHandle, childWindowFinder.EnumWindowsProcToChildWindowByCaption);
            var handle = childWindowFinder.ResultHandle;

            if (handle != IntPtr.Zero)
            {
                NativeMethods.ActivateWindow(handle, mainWindowHandle);
            }
        }
Ejemplo n.º 15
0
    private async Task InterpretKeyPress(KeyPress key, CodePane codePane, CompletionPane completionPane, CancellationToken cancellationToken)
    {
        if (!completionPane.WouldKeyPressCommitCompletionItem(key))
        {
            key = await promptCallbacks.TransformKeyPressAsync(codePane.Document.GetText(), codePane.Document.Caret, key, cancellationToken).ConfigureAwait(false);
        }

        foreach (var panes in new IKeyPressHandler[] { completionPane, history, codePane })
        {
            await panes.OnKeyDown(key, cancellationToken).ConfigureAwait(false);
        }

        foreach (var panes in new IKeyPressHandler[] { completionPane, history, codePane })
        {
            await panes.OnKeyUp(key, cancellationToken).ConfigureAwait(false);
        }

        //we don't support text selection while completion list is open
        //text selection can put completion list into broken state, where filtering does not work
        //so we want this assert to be true
        Debug.Assert(!completionPane.IsOpen || (codePane.Selection is null));
    }
Ejemplo n.º 16
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="codePane"></param>
 /// <param name="selection"></param>
 public static void SetSelection(this CodePane codePane, Selection selection)
 {
     codePane.SetSelection(selection.StartLine, selection.StartColumn, selection.EndLine, selection.EndColumn);
     codePane.ForceFocus();
 }
Ejemplo n.º 17
0
 public ICodePaneWrapper Create(CodePane codePane)
 {
     return(new CodePaneWrapper(codePane));
 }
Ejemplo n.º 18
0
 public CodePaneWrapper(CodePane codePane)
 {
     // bug: if there's no active code pane, we're creating (and using) an invalid object -> NullReferenceException
     _codePane = codePane;
 }
Ejemplo n.º 19
0
 public CodePaneWrapper(CodePane codePane)
 {
     // bug: if there's no active code pane, we're creating (and using) an invalid object -> NullReferenceException
     _codePane = codePane;
 }
 public ICodePaneWrapper Create(CodePane codePane)
 {
     return new CodePaneWrapper(codePane);
 }
Ejemplo n.º 21
0
 private static Selection GetSelection(CodePane codePane)
 {
     int startLine;
     int startColumn;
     int endLine;
     int endColumn;
     codePane.GetSelection(out startLine, out startColumn, out endLine, out endColumn);
     return new Selection(startLine, startColumn, endLine, endColumn);
 }
Ejemplo n.º 22
0
 private bool EnoughRoomToDisplay(CodePane codePane) =>
 codePane.CodeAreaHeight - codePane.Cursor.Row >= VerticalPaddingHeight + configuration.MinCompletionItemsCount;     // offset + top border + MinCompletionItemsCount + bottom border
Ejemplo n.º 23
0
 public SelectionKeyPressHandler(CodePane codePane)
 {
     this.codePane = codePane;
 }
Ejemplo n.º 24
0
        public Declaration FindSelectedDeclaration(CodePane activeCodePane)
        {
            var selection = activeCodePane.GetSelection();
            if (selection.Equals(_lastSelection))
            {
                return _selectedDeclaration;
            }

            _lastSelection = selection;
            _selectedDeclaration = null;

            if (!selection.Equals(default(QualifiedSelection)))
            {
                var matches = AllDeclarations
                    .Where(item => item.DeclarationType != DeclarationType.Project &&
                                   item.DeclarationType != DeclarationType.ModuleOption &&
                                   item.DeclarationType != DeclarationType.Class &&
                                   item.DeclarationType != DeclarationType.Module &&
                                   (IsSelectedDeclaration(selection, item) ||
                                    item.References.Any(reference => IsSelectedReference(selection, reference))));
                try
                {
                    var match = matches.SingleOrDefault() ?? AllUserDeclarations
                        .SingleOrDefault(item => (item.DeclarationType == DeclarationType.Class || item.DeclarationType == DeclarationType.Module)
                                && item.QualifiedName.QualifiedModuleName.Equals(selection.QualifiedName));
                    _selectedDeclaration = match;
                }
                catch (InvalidOperationException exception)
                {
                    Debug.WriteLine(exception);
                }
            }

            if (_selectedDeclaration != null)
            {
                Debug.WriteLine("Current selection ({0}) is '{1}' ({2})", selection, _selectedDeclaration.IdentifierName, _selectedDeclaration.DeclarationType);
            }

            return _selectedDeclaration;
        }
Ejemplo n.º 25
0
        public Declaration FindSelectedDeclaration(CodePane activeCodePane, bool procedureLevelOnly = false)
        {
            var selection = activeCodePane.GetQualifiedSelection();

            if (selection.Equals(_lastSelection))
            {
                return(_selectedDeclaration);
            }

            if (selection == null)
            {
                return(_selectedDeclaration);
            }

            _lastSelection       = selection.Value;
            _selectedDeclaration = null;

            if (!selection.Equals(default(QualifiedSelection)))
            {
                var matches = new List <Tuple <Declaration, Selection, QualifiedModuleName> >();
                lock (_declarationSelections)
                {
                    foreach (var item in _declarationSelections)
                    {
                        if (item.Item3.Equals(selection.Value.QualifiedName) &&
                            item.Item2.ContainsFirstCharacter(selection.Value.Selection))
                        {
                            matches.Add(item);
                        }
                    }
                }
                try
                {
                    if (matches.Count == 1)
                    {
                        _selectedDeclaration = matches[0].Item1;
                    }
                    else
                    {
                        Declaration match = null;
                        if (procedureLevelOnly)
                        {
                            foreach (var item in matches)
                            {
                                if (item.Item1.DeclarationType.HasFlag(DeclarationType.Member))
                                {
                                    match = match != null ? null : item.Item1;
                                }
                            }
                        }

                        // No match
                        if (matches.Count == 0)
                        {
                            if (match == null)
                            {
                                foreach (var item in AllUserDeclarations)
                                {
                                    if ((item.DeclarationType == DeclarationType.ClassModule ||
                                         item.DeclarationType == DeclarationType.ProceduralModule) &&
                                        item.QualifiedName.QualifiedModuleName.Equals(selection.Value.QualifiedName))
                                    {
                                        match = match != null ? null : item;
                                    }
                                }
                            }
                        }
                        else
                        {
                            // Idiotic approach to find the best declaration out of a set of overlapping declarations.
                            // The one closest to the start of the user selection with the smallest width wins.
                            var userSelection = selection.Value.Selection;

                            var currentSelection = matches[0].Item2;
                            match = matches[0].Item1;

                            foreach (var item in matches)
                            {
                                var itemDifferenceInStart             = Math.Abs(userSelection.StartLine - item.Item2.StartLine);
                                var currentSelectionDifferenceInStart = Math.Abs(userSelection.StartLine - currentSelection.StartLine);

                                if (itemDifferenceInStart < currentSelectionDifferenceInStart)
                                {
                                    currentSelection = item.Item2;
                                    match            = item.Item1;
                                }

                                if (itemDifferenceInStart == currentSelectionDifferenceInStart)
                                {
                                    if (Math.Abs(userSelection.StartColumn - item.Item2.StartColumn) <
                                        Math.Abs(userSelection.StartColumn - currentSelection.StartColumn))
                                    {
                                        currentSelection = item.Item2;
                                        match            = item.Item1;
                                    }
                                }
                            }
                        }

                        _selectedDeclaration = match;
                    }
                }
                catch (InvalidOperationException exception)
                {
                    Logger.Error(exception);
                }
            }

            if (_selectedDeclaration != null)
            {
                Logger.Debug("Current selection ({0}) is '{1}' ({2})", selection, _selectedDeclaration.IdentifierName, _selectedDeclaration.DeclarationType);
            }

            return(_selectedDeclaration);
        }