-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
458 additions
and
70 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
module Marksman.CodeActions | ||
|
||
open Ionide.LanguageServerProtocol.Types | ||
open Ionide.LanguageServerProtocol.Logging | ||
|
||
open FSharpPlus.GenericBuilders | ||
open Marksman.Toc | ||
open Marksman.Workspace | ||
open Marksman.Text | ||
open Marksman.Misc | ||
|
||
open type System.Environment | ||
|
||
let private logger = LogProvider.getLoggerByName "CodeActions" | ||
|
||
type DocumentAction = { name: string; newText: string; edit: Range } | ||
|
||
let documentEdit range text documentUri : WorkspaceEdit = | ||
let textEdit = { NewText = text; Range = range } | ||
|
||
let workspaceChanges = Map.ofList [ documentUri, [| textEdit |] ] | ||
|
||
{ Changes = Some workspaceChanges; DocumentChanges = None } | ||
|
||
let tableOfContents (document: Doc) : DocumentAction option = | ||
match TableOfContents.mk document.index with | ||
| Some (toc) -> | ||
let rendered = TableOfContents.render toc | ||
let existing = TableOfContents.detect document.text | ||
|
||
let name = | ||
match existing with | ||
| None -> "Create a Table of Contents" | ||
| _ -> "Update the Table of Contents" | ||
|
||
let insertionPoint = | ||
match existing with | ||
| Some (range) -> Replacing range | ||
| None -> TableOfContents.insertionPoint document | ||
|
||
logger.trace ( | ||
Log.setMessage ("Determining table of contents insertion point") | ||
>> Log.addContext "insertionPoint" insertionPoint | ||
>> Log.addContext "existing" existing | ||
>> Log.addContext "text" rendered | ||
) | ||
|
||
let isEmpty lineNumber = document.text.LineContent(lineNumber).Trim().Length.Equals(0) | ||
|
||
let emptyLine = NewLine + NewLine | ||
let lineBreak = NewLine | ||
|
||
let (editRange, newLinesBefore, newLinesAfter) = | ||
match insertionPoint with | ||
| DocumentBeginning -> | ||
let after = | ||
if isEmpty Text.documentBeginning.Start.Line then "" else emptyLine | ||
|
||
Text.documentBeginning, "", after | ||
|
||
| Replacing range -> | ||
let before = if isEmpty (range.Start.Line - 1) then "" else emptyLine | ||
let after = if isEmpty (range.End.Line + 1) then "" else emptyLine | ||
|
||
range, before, after | ||
|
||
| After range -> | ||
let lineAfterLast = range.End.Line + 1 | ||
let newRange = Range.Mk(lineAfterLast, 0, lineAfterLast, 0) | ||
|
||
let before = if isEmpty range.End.Line then "" else lineBreak | ||
let after = if isEmpty (lineAfterLast) then lineBreak else emptyLine | ||
|
||
newRange, before, after | ||
|
||
|
||
let text = $"{newLinesBefore}{rendered}{newLinesAfter}" | ||
|
||
Some { name = name; newText = text; edit = editRange } | ||
|
||
| _ -> None |
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
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
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
Oops, something went wrong.