-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Provide reference lens data for VSCode
When client's experimental.codeLensFindReferences capability is set (only VSCode plugin now), Marksman will output extra data for the code lens command that can be used to implement client-side behavior to show references when clicking on the lens.
- Loading branch information
1 parent
c8b9ac2
commit f124c56
Showing
4 changed files
with
94 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,58 @@ | ||
module Marksman.Lenses | ||
|
||
open Ionide.LanguageServerProtocol.Types | ||
module LspServer = Ionide.LanguageServerProtocol.Server | ||
|
||
open Marksman.Doc | ||
open Marksman.Folder | ||
open Marksman.State | ||
open Marksman.Refs | ||
|
||
let findReferencesLens = "marksman.findReferences" | ||
|
||
// fsharplint:disable-next-line | ||
type FindReferencesData = { Uri: DocumentUri; Position: Position; Locations: Location[] } | ||
|
||
let private humanRefCount cnt = if cnt = 1 then "1 reference" else $"{cnt} references" | ||
|
||
let forDoc (folder: Folder) (doc: Doc) = | ||
seq { | ||
// Process headers | ||
for h in doc.Index.headings do | ||
let refCount = | ||
Dest.findElementRefs false folder doc (Cst.H h) |> Seq.length | ||
|
||
if refCount > 0 then | ||
let command = | ||
{ Title = humanRefCount refCount | ||
Command = findReferencesLens | ||
Arguments = None } | ||
|
||
yield { Range = h.range; Command = Some command; Data = None } | ||
|
||
// Process link defs | ||
for ld in doc.Index.linkDefs do | ||
let refCount = | ||
Dest.findElementRefs false folder doc (Cst.MLD ld) |> Seq.length | ||
|
||
if refCount > 0 then | ||
let command = | ||
{ Title = humanRefCount refCount | ||
Command = findReferencesLens | ||
Arguments = None } | ||
|
||
yield { Range = ld.range; Command = Some command; Data = None } | ||
} | ||
|> Array.ofSeq | ||
let buildReferenceLens (client: ClientDescription) (folder: Folder) (doc: Doc) (el: Cst.Element) = | ||
let refs = Dest.findElementRefs false folder doc el |> Array.ofSeq | ||
let refCount = Array.length refs | ||
|
||
if refCount > 0 then | ||
let data = | ||
if client.SupportsLensFindReferences then | ||
let locations = | ||
[| for doc, el in refs -> { Uri = Doc.uri doc; Range = el.Range } |] | ||
|
||
let data = | ||
{ Uri = Doc.uri doc | ||
Position = el.Range.Start | ||
Locations = locations } | ||
|
||
Some [| LspServer.serialize data |] | ||
else | ||
None | ||
|
||
let command = | ||
{ Title = humanRefCount refCount | ||
Command = findReferencesLens | ||
Arguments = data } | ||
|
||
|
||
Some { Range = el.Range; Command = Some command; Data = None } | ||
else | ||
None | ||
|
||
let forDoc (client: ClientDescription) (folder: Folder) (doc: Doc) = | ||
let headingLenses = | ||
doc.Index.headings | ||
|> Seq.map Cst.H | ||
|> Seq.choose (buildReferenceLens client folder doc) | ||
|
||
let linkDefLenses = | ||
doc.Index.linkDefs | ||
|> Seq.map Cst.MLD | ||
|> Seq.choose (buildReferenceLens client folder doc) | ||
|
||
Seq.append headingLenses linkDefLenses |> Array.ofSeq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters