/// <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); }
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)); }
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)); }
/// <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); }
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); }
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); }
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(); }
/// <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)); }
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)); }
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); }
/// <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); }
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); } }
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)); }
/// <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(); }
public ICodePaneWrapper Create(CodePane codePane) { return(new CodePaneWrapper(codePane)); }
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); }
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); }
private bool EnoughRoomToDisplay(CodePane codePane) => codePane.CodeAreaHeight - codePane.Cursor.Row >= VerticalPaddingHeight + configuration.MinCompletionItemsCount; // offset + top border + MinCompletionItemsCount + bottom border
public SelectionKeyPressHandler(CodePane codePane) { this.codePane = codePane; }
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; }
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); }