private int?GetDesiredIndentation(ITextSnapshotLine line, CancellationToken cancellationToken) { if (line == null) { throw new ArgumentNullException(nameof(line)); } using (Logger.LogBlock(FunctionId.SmartIndentation_Start, cancellationToken)) { var document = line.Snapshot.GetOpenDocumentInCurrentContextWithChanges(); if (document == null) { return(null); } var newService = document.GetLanguageService <IIndentationService>(); if (newService == null) { return(null); } var indentationOptions = _globalOptions.GetIndentationOptionsAsync(document, cancellationToken).WaitAndGetResult_CanCallOnBackground(cancellationToken); var result = newService.GetIndentation(document, line.LineNumber, indentationOptions, cancellationToken); return(result.GetIndentation(_textView, line)); } }
private bool SplitString(ITextView textView, ITextBuffer subjectBuffer, int position, bool useTabs, int tabSize, CancellationToken cancellationToken) { var document = subjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges(); if (document == null) { return(false); } // TODO: read option from textView.Options (https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1412138) var options = _globalOptions.GetIndentationOptionsAsync(document, cancellationToken).WaitAndGetResult(cancellationToken); using var transaction = CaretPreservingEditTransaction.TryCreate( CSharpEditorResources.Split_string, textView, _undoHistoryRegistry, _editorOperationsFactoryService); var splitter = StringSplitter.TryCreate(document, position, options, useTabs, tabSize, cancellationToken); if (splitter?.TrySplit(out var newDocument, out var newPosition) != true) { return(false); } // apply the change: var workspace = newDocument.Project.Solution.Workspace; workspace.TryApplyChanges(newDocument.Project.Solution); // move caret: var snapshotPoint = new SnapshotPoint( subjectBuffer.CurrentSnapshot, newPosition); var newCaretPoint = textView.BufferGraph.MapUpToBuffer( snapshotPoint, PointTrackingMode.Negative, PositionAffinity.Predecessor, textView.TextBuffer); if (newCaretPoint != null) { textView.Caret.MoveTo(newCaretPoint.Value); } transaction.Complete(); return(true); }
private bool TryStart(CancellationToken cancellationToken) { _threadingContext.ThrowIfNotOnUIThread(); var closingSnapshotPoint = ClosingPoint.GetPoint(SubjectBuffer.CurrentSnapshot); if (closingSnapshotPoint.Position < 1) { Debug.Fail("The closing point was not found at the expected position."); return(false); } var openingSnapshotPoint = closingSnapshotPoint.Subtract(1); if (openingSnapshotPoint.GetChar() != OpeningBrace) { // there is a bug in editor brace completion engine on projection buffer that already fixed in vs_pro. until that is FIed to use // I will make this not to assert // Debug.Fail("The opening brace was not found at the expected position."); return(false); } OpeningPoint = SubjectBuffer.CurrentSnapshot.CreateTrackingPoint(openingSnapshotPoint, PointTrackingMode.Positive); var context = GetBraceCompletionContext(); if (context == null) { return(false); } var braceResult = _service.GetBraceCompletionAsync(context.Value, cancellationToken).WaitAndGetResult(cancellationToken); if (braceResult == null) { return(false); } using var caretPreservingTransaction = new CaretPreservingEditTransaction(EditorFeaturesResources.Brace_Completion, _undoHistory, _editorOperations); // Apply the change to complete the brace. ApplyBraceCompletionResult(braceResult.Value); // switch the closing point from positive to negative tracking so that the closing point stays against the closing brace ClosingPoint = SubjectBuffer.CurrentSnapshot.CreateTrackingPoint(ClosingPoint.GetPoint(SubjectBuffer.CurrentSnapshot), PointTrackingMode.Negative); var contextAfterStart = GetBraceCompletionContext(); if (contextAfterStart != null) { var document = contextAfterStart.Value.Document; var indentationOptions = _globalOptions.GetIndentationOptionsAsync(document, cancellationToken).WaitAndGetResult(cancellationToken); var changesAfterStart = _service.GetTextChangesAfterCompletionAsync(contextAfterStart.Value, indentationOptions, cancellationToken).WaitAndGetResult(cancellationToken); if (changesAfterStart != null) { ApplyBraceCompletionResult(changesAfterStart.Value); } } caretPreservingTransaction.Complete(); return(true); }