string ISendToInteractiveSubmissionProvider.GetSelectedText(IEditorOptions editorOptions, EditorCommandArgs args, CancellationToken cancellationToken) { var selectedSpans = args.TextView.Selection.IsEmpty ? GetExpandedLine(editorOptions, args, cancellationToken) : args.TextView.Selection.GetSnapshotSpansOnBuffer(args.SubjectBuffer).Where(ss => ss.Length > 0); return(GetSubmissionFromSelectedSpans(editorOptions, selectedSpans)); }
private void CommitIfActiveAndCallNextHandler(EditorCommandArgs args, Action nextHandler) { CommitIfActive(args); nextHandler(); }
protected static bool AreSnippetsEnabled(EditorCommandArgs args) { return(args.SubjectBuffer.GetFeatureOnOffOption(InternalFeatureOnOffOptions.Snippets) && // TODO (https://github.com/dotnet/roslyn/issues/5107): enable in interactive !(Workspace.TryGetWorkspace(args.SubjectBuffer.AsTextContainer(), out var workspace) && workspace.Kind == WorkspaceKind.Interactive)); }
private bool TryGetController(EditorCommandArgs args, out Controller controller) { return(_completionService.TryGetController(args.TextView, args.SubjectBuffer, out controller)); }
public void ExecuteReturnOrTypeCommand(EditorCommandArgs args, Action nextHandler, CancellationToken cancellationToken) { // run next handler first so that editor has chance to put the return into the buffer first. nextHandler(); var textView = args.TextView; var subjectBuffer = args.SubjectBuffer; if (!CanExecuteCommand(subjectBuffer)) { return; } var caretPosition = textView.GetCaretPoint(args.SubjectBuffer); if (!caretPosition.HasValue) { return; } var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges(); if (document == null) { return; } var service = document.GetLanguageService <IEditorFormattingService>(); if (service == null) { return; } IList <TextChange>?textChanges; // save current caret position if (args is ReturnKeyCommandArgs) { if (!service.SupportsFormatOnReturn) { return; } textChanges = service.GetFormattingChangesOnReturnAsync(document, caretPosition.Value, cancellationToken).WaitAndGetResult(cancellationToken); } else if (args is TypeCharCommandArgs typeCharArgs) { if (!service.SupportsFormattingOnTypedCharacter(document, typeCharArgs.TypedChar)) { return; } textChanges = service.GetFormattingChangesAsync(document, typeCharArgs.TypedChar, caretPosition.Value, cancellationToken).WaitAndGetResult(cancellationToken); } else { throw ExceptionUtilities.UnexpectedValue(args); } if (textChanges == null || textChanges.Count == 0) { return; } using (var transaction = CreateEditTransaction(textView, EditorFeaturesResources.Automatic_Formatting)) { transaction.MergePolicy = AutomaticCodeChangeMergePolicy.Instance; document.Project.Solution.Workspace.ApplyTextChanges(document.Id, textChanges, cancellationToken); transaction.Complete(); } // get new caret position after formatting var newCaretPositionMarker = args.TextView.GetCaretPoint(args.SubjectBuffer); if (!newCaretPositionMarker.HasValue) { return; } var snapshotAfterFormatting = args.SubjectBuffer.CurrentSnapshot; var oldCaretPosition = caretPosition.Value.TranslateTo(snapshotAfterFormatting, PointTrackingMode.Negative); var newCaretPosition = newCaretPositionMarker.Value.TranslateTo(snapshotAfterFormatting, PointTrackingMode.Negative); if (oldCaretPosition.Position == newCaretPosition.Position) { return; } // caret has moved to wrong position, move it back to correct position args.TextView.TryMoveCaretToAndEnsureVisible(oldCaretPosition); }
public EditorCommandHandlerServiceState(EditorCommandArgs executingCommand, bool isTypingCommand) { _executingCommandHandlers = new ConcurrentStack <ICommandHandler>(); ExecutingCommand = executingCommand ?? throw new ArgumentNullException(nameof(executingCommand)); IsExecutingTypingCommand = isTypingCommand; }
private string GetSelectedText(EditorCommandArgs args, CancellationToken cancellationToken) { var editorOptions = _editorOptionsFactoryService.GetOptions(args.SubjectBuffer); return(SendToInteractiveSubmissionProvider.GetSelectedText(editorOptions, args, cancellationToken)); }
public void ExecuteReturnOrTypeCommand(EditorCommandArgs args, Action nextHandler, CancellationToken cancellationToken) { // This method handles only return / type char if (!(args is ReturnKeyCommandArgs || args is TypeCharCommandArgs)) { return; } // run next handler first so that editor has chance to put the return into the buffer first. nextHandler(); var textView = args.TextView; var subjectBuffer = args.SubjectBuffer; if (!subjectBuffer.CanApplyChangeDocumentToWorkspace()) { return; } var caretPosition = textView.GetCaretPoint(args.SubjectBuffer); if (!caretPosition.HasValue) { return; } var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges(); if (document == null) { return; } var service = document.GetLanguageService <IEditorFormattingService>(); if (service == null) { return; } // save current caret position var caretPositionMarker = new SnapshotPoint(args.SubjectBuffer.CurrentSnapshot, caretPosition.Value); if (args is ReturnKeyCommandArgs) { if (!service.SupportsFormatOnReturn || !TryFormat(textView, document, service, ' ', caretPositionMarker, formatOnReturn: true, cancellationToken: cancellationToken)) { return; } } else if (args is TypeCharCommandArgs typeCharArgs) { var typedChar = typeCharArgs.TypedChar; if (!service.SupportsFormattingOnTypedCharacter(document, typedChar) || !TryFormat(textView, document, service, typedChar, caretPositionMarker, formatOnReturn: false, cancellationToken: cancellationToken)) { return; } } // get new caret position after formatting var newCaretPositionMarker = args.TextView.GetCaretPoint(args.SubjectBuffer); if (!newCaretPositionMarker.HasValue) { return; } var snapshotAfterFormatting = args.SubjectBuffer.CurrentSnapshot; var oldCaretPosition = caretPositionMarker.TranslateTo(snapshotAfterFormatting, PointTrackingMode.Negative); var newCaretPosition = newCaretPositionMarker.Value.TranslateTo(snapshotAfterFormatting, PointTrackingMode.Negative); if (oldCaretPosition.Position == newCaretPosition.Position) { return; } // caret has moved to wrong position, move it back to correct position args.TextView.TryMoveCaretToAndEnsureVisible(oldCaretPosition); }