2
2
{-# LANGUAGE TemplateHaskell #-}
3
3
{-# LANGUAGE TypeOperators #-}
4
4
{-# LANGUAGE OverloadedStrings #-}
5
+ {-# LANGUAGE RecordWildCards #-}
5
6
6
7
module Language.LSP.Types.WorkspaceEdit where
7
8
9
+ import Control.Monad (unless )
8
10
import Data.Aeson
9
11
import Data.Aeson.TH
10
12
import qualified Data.HashMap.Strict as H
13
+ import Data.Maybe (catMaybes )
11
14
import Data.Text (Text )
12
15
import qualified Data.Text as T
13
16
@@ -40,25 +43,6 @@ deriveJSON lspOptions ''TextDocumentEdit
40
43
41
44
-- ---------------------------------------------------------------------
42
45
43
- -- | For tagging `CreateFile`/`RenameFile`/`DeleteFile`
44
- -- Should this be merged with `ResourceOperationKind` ?
45
- data FileResourceChangeKind
46
- = FileResourceChangeCreate
47
- | FileResourceChangeRename
48
- | FileResourceChangeDelete
49
- deriving (Read , Show , Eq )
50
-
51
- instance ToJSON FileResourceChangeKind where
52
- toJSON FileResourceChangeCreate = String " create"
53
- toJSON FileResourceChangeRename = String " rename"
54
- toJSON FileResourceChangeDelete = String " delete"
55
-
56
- instance FromJSON FileResourceChangeKind where
57
- parseJSON (String " create" ) = pure FileResourceChangeCreate
58
- parseJSON (String " rename" ) = pure FileResourceChangeRename
59
- parseJSON (String " delete" ) = pure FileResourceChangeDelete
60
- parseJSON _ = mempty
61
-
62
46
-- | Options to create a file.
63
47
data CreateFileOptions =
64
48
CreateFileOptions
@@ -73,14 +57,28 @@ deriveJSON lspOptions ''CreateFileOptions
73
57
-- | Create file operation
74
58
data CreateFile =
75
59
CreateFile
76
- { _kind :: FileResourceChangeKind
77
- -- | The resource to create.
78
- , _uri :: Text
60
+ { -- | The resource to create.
61
+ _uri :: Text
79
62
-- | Additional options
80
63
, _options :: Maybe CreateFileOptions
81
64
} deriving (Show , Read , Eq )
82
65
83
- deriveJSON lspOptions ''CreateFile
66
+ instance ToJSON CreateFile where
67
+ toJSON CreateFile {.. } =
68
+ object $ catMaybes
69
+ [ Just $ " kind" .= (" create" :: Text )
70
+ , Just $ " uri" .= _uri
71
+ , (" options" .= ) <$> _options
72
+ ]
73
+
74
+ instance FromJSON CreateFile where
75
+ parseJSON = withObject " CreateFile" $ \ o -> do
76
+ kind <- o .: " kind"
77
+ unless (kind == (" create" :: Text ))
78
+ $ fail $ " Expected kind \" create\" but got " ++ show kind
79
+ _uri <- o .: " uri"
80
+ _options <- o .:? " options"
81
+ pure CreateFile {.. }
84
82
85
83
-- Rename file options
86
84
data RenameFileOptions =
@@ -96,16 +94,32 @@ deriveJSON lspOptions ''RenameFileOptions
96
94
-- | Rename file operation
97
95
data RenameFile =
98
96
RenameFile
99
- { _kind :: FileResourceChangeKind
100
- -- | The old (existing) location.
101
- , _oldUri :: Text
97
+ { -- | The old (existing) location.
98
+ _oldUri :: Text
102
99
-- | The new location.
103
100
, _newUri :: Text
104
101
-- | Rename options.
105
102
, _options :: Maybe RenameFileOptions
106
103
} deriving (Show , Read , Eq )
107
104
108
- deriveJSON lspOptions ''RenameFile
105
+ instance ToJSON RenameFile where
106
+ toJSON RenameFile {.. } =
107
+ object $ catMaybes
108
+ [ Just $ " kind" .= (" rename" :: Text )
109
+ , Just $ " oldUri" .= _oldUri
110
+ , Just $ " newUri" .= _newUri
111
+ , (" options" .= ) <$> _options
112
+ ]
113
+
114
+ instance FromJSON RenameFile where
115
+ parseJSON = withObject " RenameFile" $ \ o -> do
116
+ kind <- o .: " kind"
117
+ unless (kind == (" rename" :: Text ))
118
+ $ fail $ " Expected kind \" rename\" but got " ++ show kind
119
+ _oldUri <- o .: " oldUri"
120
+ _newUri <- o .: " newUri"
121
+ _options <- o .:? " options"
122
+ pure RenameFile {.. }
109
123
110
124
-- Delete file options
111
125
data DeleteFileOptions =
@@ -121,14 +135,29 @@ deriveJSON lspOptions ''DeleteFileOptions
121
135
-- | Delete file operation
122
136
data DeleteFile =
123
137
DeleteFile
124
- { _kind :: FileResourceChangeKind
125
- -- | The file to delete.
126
- , _uri :: Text
138
+ { -- | The file to delete.
139
+ _uri :: Text
127
140
-- | Delete options.
128
141
, _options :: Maybe DeleteFileOptions
129
142
} deriving (Show , Read , Eq )
130
143
131
- deriveJSON lspOptions ''DeleteFile
144
+ instance ToJSON DeleteFile where
145
+ toJSON DeleteFile {.. } =
146
+ object $ catMaybes
147
+ [ Just $ " kind" .= (" delete" :: Text )
148
+ , Just $ " uri" .= _uri
149
+ , (" options" .= ) <$> _options
150
+ ]
151
+
152
+ instance FromJSON DeleteFile where
153
+ parseJSON = withObject " DeleteFile" $ \ o -> do
154
+ kind <- o .: " kind"
155
+ unless (kind == (" delete" :: Text ))
156
+ $ fail $ " Expected kind \" delete\" but got " ++ show kind
157
+ _uri <- o .: " uri"
158
+ _options <- o .:? " options"
159
+ pure DeleteFile {.. }
160
+
132
161
133
162
-- ---------------------------------------------------------------------
134
163
0 commit comments