Skip to content

Commit b542aee

Browse files
xperiandriclaude
andcommitted
Compact the snippet support
- Directive and string-continuation lines are what the F# lexer says they are: the hand-written directive prefixes and lexer-state list are gone. The snippet's own lines and the selected ones are lexed apart, so a region the selection leaves open no longer moves the wrapper's `#endif`, and `$$"""` continuations are recognised. - Surround With takes the wrapper's column from the selected code only, not from directive lines or string content in it. - `FormatSpan` delegates to a `Reindent` built from named steps instead of a narrated comment. - `ClassName()` walks the parse tree with `ParsedInput.tryPickLast`, and also answers in signature files; `GenerateMatchCases()` uses `DisplayName`, so cases that need backticks get them. - Editor options, out-parameter tuples, `RoslynHelpers` range conversions and `tryGetDocument` are reused instead of re-implemented; the pending Surround With selection is passed along rather than reset in three places. - Comments that restated the code are removed; tests are table-driven. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7797f9d commit b542aee

8 files changed

Lines changed: 585 additions & 871 deletions

File tree

‎vsintegration/src/FSharp.Editor/Common/Vs.fs‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,9 @@ module internal ServiceProviderExtensions =
101101
match sp.GetService<SVsTextManager, IVsTextManager2>() with
102102
| null -> null
103103
| textManager ->
104-
let mutable expansionManager = Unchecked.defaultof<IVsExpansionManager>
105-
106-
if Com.Succeeded(textManager.GetExpansionManager(&expansionManager)) then
107-
expansionManager
108-
else
109-
null
104+
match textManager.GetExpansionManager() with
105+
| hr, expansionManager when Com.Succeeded hr -> expansionManager
106+
| _ -> null
110107

111108
member sp.RunningDocumentTable =
112109
sp.GetService<SVsRunningDocumentTable, IVsRunningDocumentTable>()

‎vsintegration/src/FSharp.Editor/Snippets/SnippetCommandHandler.fs‎

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22

33
namespace Microsoft.VisualStudio.FSharp.Editor
44

5-
open System
65
open System.ComponentModel.Composition
76

87
open Microsoft.CodeAnalysis
9-
open Microsoft.CodeAnalysis.Text
108

119
open Microsoft.VisualStudio.Commanding
1210
open Microsoft.VisualStudio.Editor
1311
open Microsoft.VisualStudio.Language.Intellisense.AsyncCompletion
1412
open Microsoft.VisualStudio.Text
1513
open Microsoft.VisualStudio.Text.Editor
1614
open Microsoft.VisualStudio.Text.Editor.Commanding.Commands
15+
open Microsoft.VisualStudio.Text.Editor.OptionsExtensionMethods
1716
open Microsoft.VisualStudio.Utilities
1817

19-
open FSharp.Compiler.EditorServices
20-
2118
open CancellableTasks
2219

2320
[<AutoOpen>]
@@ -26,9 +23,8 @@ module internal SnippetCommandHelpers =
2623
[<Literal>]
2724
let private userOpName = "FSharpSnippetCommandHandler"
2825

29-
/// The snippet shortcut the caret is sitting at the end of. Going through the lexer is what keeps
30-
/// Tab from expanding a word typed inside a string or a comment, and `FullIsland` is what keeps it
31-
/// from expanding the member name in `value.for`.
26+
/// Going through the lexer is what keeps Tab from expanding a word typed inside a string or a
27+
/// comment, and `FullIsland` is what keeps it from expanding the member name in `value.for`.
3228
let tryGetShortcutAt (document: Document) position =
3329
cancellableTask {
3430
let! symbol = document.TryFindFSharpLexerSymbolAsync(position, SymbolLookupKind.Greedy, false, false, userOpName)
@@ -39,8 +35,6 @@ module internal SnippetCommandHelpers =
3935
| _ -> ValueNone
4036
}
4137

42-
/// Drops a trailing line break from the selection, changing what is selected and nothing else.
43-
///
4438
/// Selecting whole lines ends the selection at column 0 of the following one, so `$selected$`
4539
/// receives that line break too: whatever the snippet places after the field - `#endif`, `else`,
4640
/// a closing `}` - lands on the line that followed the selection instead of on its own.
@@ -59,36 +53,28 @@ module internal SnippetCommandHelpers =
5953

6054
/// The column the wrapped code sits at and how many lines it covers. The insertion replaces the
6155
/// selection, so neither survives it and the expansion client is told up front.
62-
///
63-
/// The column is the narrowest indentation in the block, not the first line's: the wrapper belongs
64-
/// at the block's own left edge even when the block starts with a deeper line.
6556
let selectionShape (textView: ITextView) =
6657
let span = textView.Selection.StreamSelectionSpan.SnapshotSpan
6758
let snapshot = span.Snapshot
68-
let firstLine = snapshot.GetLineFromPosition span.Start.Position
69-
let lastLine = snapshot.GetLineFromPosition span.End.Position
70-
let tabSize = tabSizeOf textView.Options
71-
72-
let column =
73-
seq { firstLine.LineNumber .. lastLine.LineNumber }
74-
|> Seq.fold
75-
(fun narrowest lineNumber ->
76-
let line = snapshot.GetLineFromLineNumber lineNumber
59+
let firstLine = snapshot.GetLineFromPosition(span.Start.Position).LineNumber
60+
let lastLine = snapshot.GetLineFromPosition(span.End.Position).LineNumber
7761

78-
match leadingWhitespaceOf line with
79-
| leadingWhitespace when leadingWhitespace = line.Length -> narrowest
80-
| leadingWhitespace -> min narrowest (visualColumnAt tabSize line leadingWhitespace))
81-
Int32.MaxValue
62+
let lines =
63+
[
64+
for lineNumber in firstLine..lastLine -> snapshot.GetLineFromLineNumber(lineNumber).GetText()
65+
]
8266

83-
let column = if column = Int32.MaxValue then 0 else column
67+
let column =
68+
SnippetIndentation.classify (textView.Options.GetTabSize()) (ValueSome(0, lines.Length - 1)) lines
69+
|> List.filter (fun line -> line.Kind.IsSelectedFirst || line.Kind.IsSelectedRest)
70+
|> List.map _.Indent
71+
|> function
72+
| [] -> 0
73+
| indents -> List.min indents
8474

85-
column, lastLine.LineNumber - firstLine.LineNumber + 1
75+
column, lines.Length
8676

87-
/// Insert Snippet (Ctrl+K,Ctrl+X), Surround With (Ctrl+K,Ctrl+S), Tab expansion of a snippet
88-
/// shortcut, and the keys that drive a live expansion session.
89-
///
90-
/// Ordered after the completion handler so that Tab still commits an open completion list first,
91-
/// which is how the C# handler is ordered too.
77+
/// Ordered after the completion handler so that Tab still commits an open completion list first.
9278
[<Export(typeof<ICommandHandler>)>]
9379
[<ContentType(FSharpConstants.FSharpContentTypeName)>]
9480
[<Name(Constants.FSharpSnippetsCommandHandler)>]
@@ -104,31 +90,25 @@ type internal FSharpSnippetCommandHandler [<ImportingConstructor>] (editorAdapte
10490
)
10591
| _ -> ValueNone
10692

107-
/// Only a live session gets to see Tab, Shift+Tab, Enter and Escape.
10893
let tryGetSessionClient (textView: ITextView) (subjectBuffer: ITextBuffer) =
10994
tryGetClient textView subjectBuffer |> ValueOption.filter _.IsInSession
11095

11196
let tryExpandShortcut (args: TabKeyCommandArgs) (client: FSharpSnippetExpansionClient) =
112-
match args.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges() with
113-
| null -> false
114-
| document when not document.Project.IsFSharp -> false
115-
| document ->
116-
let caret = args.TextView.Caret.Position.BufferPosition.Position
117-
118-
match runSynchronously parseTimeout (tryGetShortcutAt document caret) with
119-
| ValueNone -> false
120-
| ValueSome(shortcut, range) ->
121-
let shortcutSpan =
122-
VsTextSpan(
123-
iStartLine = range.StartLine - 1,
124-
iStartIndex = range.StartColumn,
125-
iEndLine = range.EndLine - 1,
126-
iEndIndex = range.EndColumn
127-
)
128-
129-
client.TryInsertExpansionForShortcut(shortcut, shortcutSpan)
130-
131-
// `ICommandHandler<_>` inherits `INamed`, so the name is given once for all six of them.
97+
let caret = args.TextView.Caret.Position.BufferPosition.Position
98+
99+
tryGetDocument args.SubjectBuffer
100+
|> ValueOption.bind (fun document -> runSynchronously parseTimeout (tryGetShortcutAt document caret))
101+
|> ValueOption.exists (fun (shortcut, range) ->
102+
let shortcutSpan =
103+
VsTextSpan(
104+
iStartLine = range.StartLine - 1,
105+
iStartIndex = range.StartColumn,
106+
iEndLine = range.EndLine - 1,
107+
iEndIndex = range.EndColumn
108+
)
109+
110+
client.TryInsertExpansionForShortcut(shortcut, shortcutSpan))
111+
132112
interface INamed with
133113
member _.DisplayName = Constants.FSharpSnippetsCommandHandler
134114

@@ -146,8 +126,7 @@ type internal FSharpSnippetCommandHandler [<ImportingConstructor>] (editorAdapte
146126
else
147127
CommandState.Available
148128

149-
// The buffer is left alone - the expansion engine reads the selection off the view to fill
150-
// `$selected$`, and editing first was what yanked the code to column 0.
129+
// The buffer is left alone: the expansion engine reads the selection off the view to fill `$selected$`.
151130
member _.ExecuteCommand(args, _) =
152131
trimSelectedLineBreak args.TextView
153132
let column, lineCount = selectionShape args.TextView

0 commit comments

Comments
 (0)