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

Commit 51b91f5

Browse files
cartermpnosami
authored andcommitted
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 0e260bc commit 51b91f5

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
@@ -188,19 +188,30 @@ module internal OpenDeclarationHelper =
188188

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

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)