@@ -19,18 +19,16 @@ module Language.Haskell.LSP.VFS
19
19
-- * for tests
20
20
, applyChange
21
21
, sortChanges
22
- , deleteChars , addChars
23
22
, changeChars
24
- , yiSplitAt
25
23
) where
26
24
27
25
import Data.Text ( Text )
28
26
import Data.List
29
- import Data.Monoid
30
27
import qualified Data.Map as Map
28
+ import Data.Rope.UTF16 ( Rope )
29
+ import qualified Data.Rope.UTF16 as Rope
31
30
import qualified Language.Haskell.LSP.TH.DataTypesJSON as J
32
31
import Language.Haskell.LSP.Utility
33
- import qualified Yi.Rope as Yi
34
32
35
33
-- ---------------------------------------------------------------------
36
34
{-# ANN module ("hlint: ignore Eta reduce" :: String) #-}
@@ -40,7 +38,7 @@ import qualified Yi.Rope as Yi
40
38
data VirtualFile =
41
39
VirtualFile {
42
40
_version :: Int
43
- , _text :: Yi. YiString
41
+ , _text :: Rope
44
42
} deriving (Show )
45
43
46
44
type VFS = Map. Map J. Uri VirtualFile
@@ -51,7 +49,7 @@ openVFS :: VFS -> J.DidOpenTextDocumentNotification -> IO VFS
51
49
openVFS vfs (J. NotificationMessage _ _ params) = do
52
50
let J. DidOpenTextDocumentParams
53
51
(J. TextDocumentItem uri _ version text) = params
54
- return $ Map. insert uri (VirtualFile version (Yi . fromText text)) vfs
52
+ return $ Map. insert uri (VirtualFile version (Rope . fromText text)) vfs
55
53
56
54
-- ---------------------------------------------------------------------
57
55
@@ -87,92 +85,37 @@ data TextDocumentContentChangeEvent =
87
85
-}
88
86
89
87
-- | Apply the list of changes, in descending order of range. Assuming no overlaps.
90
- applyChanges :: Yi. YiString -> [J. TextDocumentContentChangeEvent ] -> Yi. YiString
88
+ applyChanges :: Rope -> [J. TextDocumentContentChangeEvent ] -> Rope
91
89
applyChanges str changes' = r
92
90
where
93
91
changes = sortChanges changes'
94
92
r = foldl' applyChange str changes
95
93
96
94
-- ---------------------------------------------------------------------
97
95
98
- applyChange :: Yi. YiString -> J. TextDocumentContentChangeEvent -> Yi. YiString
96
+ applyChange :: Rope -> J. TextDocumentContentChangeEvent -> Rope
99
97
applyChange _ (J. TextDocumentContentChangeEvent Nothing Nothing str)
100
- = Yi. fromText str
101
- applyChange str (J. TextDocumentContentChangeEvent (Just (J. Range fm _to)) (Just len) txt) =
102
- if txt == " "
103
- then -- delete len chars from fm
104
- deleteChars str fm len
105
- else -- add or change, based on length
106
- if len == 0
107
- then addChars str fm txt
108
- -- Note: changeChars comes from applyEdit, emacs will split it into a
109
- -- delete and an add
110
- else changeChars str fm len txt
111
- applyChange str (J. TextDocumentContentChangeEvent (Just r@ (J. Range (J. Position sl sc) (J. Position el ec))) Nothing txt)
112
- = applyChange str (J. TextDocumentContentChangeEvent (Just r) (Just len) txt)
113
- where len = Yi. length region
114
- (beforeEnd, afterEnd) = Yi. splitAtLine el str
115
- lastLine = Yi. take ec afterEnd
116
- lastLine' | sl == el = Yi. drop sc lastLine
117
- | otherwise = lastLine
118
- (_beforeStart, afterStartBeforeEnd) = Yi. splitAtLine sl beforeEnd
119
- region = Yi. drop sc afterStartBeforeEnd <> lastLine'
120
- applyChange str (J. TextDocumentContentChangeEvent Nothing (Just _) _txt)
121
- = str
122
-
123
- -- ---------------------------------------------------------------------
124
-
125
- deleteChars :: Yi. YiString -> J. Position -> Int -> Yi. YiString
126
- deleteChars str (J. Position l c) len = str'
127
- where
128
- (before,after) = Yi. splitAtLine l str
129
- -- after contains the area we care about, starting with the selected line.
130
- -- Due to LSP zero-based coordinates
131
- beforeOnLine = Yi. take c after
132
- after' = Yi. drop (c + len) after
133
- str' = Yi. append before (Yi. append beforeOnLine after')
134
-
135
- -- ---------------------------------------------------------------------
136
-
137
- addChars :: Yi. YiString -> J. Position -> Text -> Yi. YiString
138
- addChars str (J. Position l c) new = str'
98
+ = Rope. fromText str
99
+ applyChange str (J. TextDocumentContentChangeEvent (Just (J. Range (J. Position sl sc) _to)) (Just len) txt)
100
+ = changeChars str start len txt
139
101
where
140
- (before,after) = Yi. splitAtLine l str
141
- -- after contains the area we care about, starting with the selected line.
142
- -- Due to LSP zero-based coordinates
143
- beforeOnLine = Yi. take c after
144
- after' = Yi. drop c after
145
- str' = Yi. concat [before, beforeOnLine, (Yi. fromText new), after']
146
-
147
- -- ---------------------------------------------------------------------
148
-
149
- changeChars :: Yi. YiString -> J. Position -> Int -> Text -> Yi. YiString
150
- changeChars str (J. Position ls cs) len new = str'
102
+ start = Rope. rowColumnCodePoints (Rope. RowColumn sl sc) str
103
+ applyChange str (J. TextDocumentContentChangeEvent (Just (J. Range (J. Position sl sc) (J. Position el ec))) Nothing txt)
104
+ = changeChars str start len txt
151
105
where
152
- (before,after) = yiSplitAt ls cs str
153
- after' = Yi. drop len after
154
-
155
- str' = Yi. concat [before, (Yi. fromText new), after']
156
-
157
- -- changeChars :: Yi.YiString -> J.Position -> J.Position -> String -> Yi.YiString
158
- -- changeChars str (J.Position ls cs) (J.Position le ce) new = str'
159
- -- where
160
- -- (before,_after) = yiSplitAt ls cs str
161
- -- (_before,after) = yiSplitAt le ce str
162
-
163
- -- str' = Yi.concat [before, (Yi.fromString new), after]
164
- -- -- str' = Yi.concat [before]
165
- -- -- str' = Yi.concat [_before]
106
+ start = Rope. rowColumnCodePoints (Rope. RowColumn sl sc) str
107
+ end = Rope. rowColumnCodePoints (Rope. RowColumn el ec) str
108
+ len = end - start
109
+ applyChange str (J. TextDocumentContentChangeEvent Nothing (Just _) _txt)
110
+ = str
166
111
167
112
-- ---------------------------------------------------------------------
168
113
169
- yiSplitAt :: Int -> Int -> Yi. YiString -> ( Yi. YiString , Yi. YiString )
170
- yiSplitAt l c str = ( before,after)
114
+ changeChars :: Rope -> Int -> Int -> Text -> Rope
115
+ changeChars str start len new = mconcat [ before, Rope. fromText new, after']
171
116
where
172
- (b,a) = Yi. splitAtLine l str
173
- before = Yi. concat [b,Yi. take c a]
174
- after = Yi. drop c a
175
-
117
+ (before, after) = Rope. splitAt start str
118
+ after' = Rope. drop len after
176
119
177
120
-- ---------------------------------------------------------------------
178
121
0 commit comments