From 6df569b03f0fe7433dbf8c60f3f00562a67ceed1 Mon Sep 17 00:00:00 2001 From: "arunkumar.a" Date: Mon, 25 May 2026 18:44:29 +0530 Subject: [PATCH] Fix code action optional fields --- .../IdePurescript/CodeActions.purs | 8 +++---- src/LanguageServer/Protocol/Types.purs | 4 ++-- test/Main.js | 1 + test/Main.purs | 22 ++++++++++++++++++- 4 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 test/Main.js diff --git a/src/LanguageServer/IdePurescript/CodeActions.purs b/src/LanguageServer/IdePurescript/CodeActions.purs index 1a9a9fd..dd4a79a 100644 --- a/src/LanguageServer/IdePurescript/CodeActions.purs +++ b/src/LanguageServer/IdePurescript/CodeActions.purs @@ -37,6 +37,7 @@ import LanguageServer.Protocol.Text (makeWorkspaceEdit) import LanguageServer.Protocol.TextDocument (TextDocument, getTextAtRange, getVersion) import LanguageServer.Protocol.Types (ClientCapabilities, CodeAction(..), CodeActionKind(..), CodeActionResult, Command(..), DocumentStore, DocumentUri(DocumentUri), OptionalVersionedTextDocumentIdentifier(..), Position(Position), Range(Range), Settings, TextDocumentEdit(..), TextDocumentIdentifier(TextDocumentIdentifier), TextEdit(..), codeActionEmpty, codeActionResult, codeActionSourceOrganizeImports, codeActionSourceSortImports, readRange, workspaceEdit) import PscIde.Command (PscSuggestion(..), PursIdeInfo(..), RebuildError(..)) +import Untagged.Union (maybeToUor, uorToMaybe) getActions :: DocumentStore -> @@ -200,8 +201,7 @@ codeActionToCommand capabilities action = convert action where supportsLiteral = maybe true codeActionLiteralsSupported capabilities - convert (CodeAction { command }) | Just c <- Nullable.toMaybe command = Just $ - Right c + convert (CodeAction { command }) | Just c <- uorToMaybe command = Just $ Right c convert _ = Nothing commandAction :: CodeActionKind -> Command -> CodeAction @@ -210,8 +210,8 @@ commandAction kind c@(Command { title }) = { title , kind , isPreferred: false - , edit: Nullable.toNullable Nothing - , command: Nullable.toNullable $ Just c + , edit: maybeToUor Nothing + , command: maybeToUor $ Just c } commandAction_ :: Command -> CodeAction diff --git a/src/LanguageServer/Protocol/Types.purs b/src/LanguageServer/Protocol/Types.purs index 4d976b3..6fad5c3 100644 --- a/src/LanguageServer/Protocol/Types.purs +++ b/src/LanguageServer/Protocol/Types.purs @@ -272,8 +272,8 @@ newtype CodeAction = CodeAction { title :: String , kind :: CodeActionKind , isPreferred :: Boolean - , edit :: Nullable WorkspaceEdit - , command :: Nullable Command + , edit :: UndefinedOr WorkspaceEdit + , command :: UndefinedOr Command } foreign import data CodeActionResult :: Type diff --git a/test/Main.js b/test/Main.js new file mode 100644 index 0000000..d63fbc4 --- /dev/null +++ b/test/Main.js @@ -0,0 +1 @@ +export const stringifyCodeAction = (action) => JSON.stringify(action); diff --git a/test/Main.purs b/test/Main.purs index 094d353..ce79208 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -7,13 +7,17 @@ import Data.Maybe (Maybe(..), fromMaybe) import Data.Nullable (null, toMaybe) import Data.Nullable as Nullable import Effect (Effect) +import Foreign (unsafeToForeign) import IdePurescript.Tokens (identifierAtPoint) import LanguageServer.IdePurescript.FileTypes (RelevantFileType(..), jsUriToMayPsUri, uriToRelevantFileType) import LanguageServer.Protocol.Text (makeMinimalWorkspaceEdit) -import LanguageServer.Protocol.Types (ClientCapabilities, DocumentUri(..), Position(..), Range(..), TextDocumentEdit(..), TextEdit(..), WorkspaceEdit(..)) +import LanguageServer.Protocol.Types (ClientCapabilities, CodeAction(..), Command(..), DocumentUri(..), Position(..), Range(..), TextDocumentEdit(..), TextEdit(..), WorkspaceEdit(..), codeActionSourceSortImports) import Test.Unit (suite, test) import Test.Unit.Assert as Assert import Test.Unit.Main (runTest) +import Untagged.Union (maybeToUor) + +foreign import stringifyCodeAction :: CodeAction -> String getEdit :: WorkspaceEdit -> Array TextEdit getEdit (WorkspaceEdit { documentChanges }) = concat $ map go $ changes @@ -76,6 +80,22 @@ main = -- If I have a CRLF file for some reason but IDE server gives me back LF let edit = makeEdit "A\r\nC\r\n" "A\nB\nC\n" Assert.equal (Just [ mkEdit 1 1 "B\n" ]) (getEdit <$> edit) + test "command-only code action omits edit when serialized" do + let + action = CodeAction + { title: "Sort/reformat imports" + , kind: codeActionSourceSortImports + , isPreferred: false + , edit: maybeToUor Nothing + , command: maybeToUor $ Just $ Command + { title: "Sort/reformat imports" + , command: "sortImports" + , arguments: Nullable.notNull [ unsafeToForeign "uri" ] + } + } + Assert.equal + """{"title":"Sort/reformat imports","kind":"source.sortImports","isPreferred":false,"command":{"title":"Sort/reformat imports","command":"sortImports","arguments":["uri"]}}""" + (stringifyCodeAction action) -- Type at point test "identifierAtPoint: identifies $" do let str = """$"""