Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit 91447b8

Browse files
authored
Use roslyn API to display extended completion and fix quirks with scripts/top of file (dotnet#9165)
* Use roslyn API to show unopened namespace in extended completion * Properly add open decls when at the top of a file
1 parent cdf8db6 commit 91447b8

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

Common/RoslynHelpers.fs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,30 @@ module internal OpenDeclarationHelper =
187187

188188
let getLineStr line = sourceText.Lines.[line].ToString().Trim()
189189
let pos = ParsedInput.adjustInsertionPoint getLineStr ctx
190-
let docLine = pos.Line - 1
190+
let docLine = Line.toZ pos.Line
191191
let lineStr = (String.replicate pos.Column " ") + "open " + ns
192-
let sourceText = sourceText |> insert docLine lineStr
192+
193+
// If we're at the top of a file (e.g., F# script) then add a newline before adding the open declaration
194+
let sourceText =
195+
if docLine = 0 then
196+
sourceText
197+
|> insert docLine Environment.NewLine
198+
|> insert docLine lineStr
199+
else
200+
sourceText |> insert docLine lineStr
201+
193202
// if there's no a blank line between open declaration block and the rest of the code, we add one
194203
let sourceText =
195204
if sourceText.Lines.[docLine + 1].ToString().Trim() <> "" then
196205
sourceText |> insert (docLine + 1) ""
197206
else sourceText
207+
198208
let sourceText =
199209
// for top level module we add a blank line between the module declaration and first open statement
200210
if (pos.Column = 0 || ctx.ScopeKind = ScopeKind.Namespace) && docLine > 0
201211
&& not (sourceText.Lines.[docLine - 1].ToString().Trim().StartsWith "open") then
202212
sourceText |> insert docLine ""
203213
else sourceText
214+
204215
sourceText, minPos |> Option.defaultValue 0
205216

Completion/CompletionProvider.fs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ type internal FSharpCompletionProvider
143143

144144
declarationItems |> Array.iteri (fun number declarationItem ->
145145
let glyph = Tokenizer.FSharpGlyphToRoslynGlyph (declarationItem.Glyph, declarationItem.Accessibility)
146-
let name =
146+
let namespaceName =
147147
match declarationItem.NamespaceToOpen with
148-
| Some namespaceToOpen -> sprintf "%s (open %s)" declarationItem.Name namespaceToOpen
149-
| _ -> declarationItem.Name
148+
| Some namespaceToOpen -> namespaceToOpen
149+
| _ -> null // Icky, but this is how roslyn handles it
150150

151151
let filterText =
152152
match declarationItem.NamespaceToOpen, declarationItem.Name.Split '.' with
@@ -157,8 +157,14 @@ type internal FSharpCompletionProvider
157157
| _, idents -> Array.last idents
158158

159159
let completionItem =
160-
FSharpCommonCompletionItem.Create(name, null, rules = getRules intellisenseOptions.ShowAfterCharIsTyped, glyph = Nullable glyph, filterText = filterText)
161-
.AddProperty(FullNamePropName, declarationItem.FullName)
160+
FSharpCommonCompletionItem.Create(
161+
declarationItem.Name,
162+
null,
163+
rules = getRules intellisenseOptions.ShowAfterCharIsTyped,
164+
glyph = Nullable glyph,
165+
filterText = filterText,
166+
inlineDescription = namespaceName)
167+
.AddProperty(FullNamePropName, declarationItem.FullName)
162168

163169
let completionItem =
164170
match declarationItem.Kind with
@@ -167,7 +173,7 @@ type internal FSharpCompletionProvider
167173
| _ -> completionItem
168174

169175
let completionItem =
170-
if name <> declarationItem.NameInCode then
176+
if declarationItem.Name <> declarationItem.NameInCode then
171177
completionItem.AddProperty(NameInCodePropName, declarationItem.NameInCode)
172178
else completionItem
173179

0 commit comments

Comments
 (0)