@@ -27,10 +27,9 @@ import qualified Data.Text as T
27
27
import Language.Haskell.LSP.Types
28
28
import Data.List
29
29
import Data.Algorithm.Diff
30
- import Data.Maybe
31
30
import Data.Bifunctor
32
31
import Control.DeepSeq
33
- import qualified Data.Vector as V
32
+ import qualified Data.Vector.Unboxed as V
34
33
35
34
-- | Either an exact position, or the range of text that was substituted
36
35
data PositionResult a
@@ -184,29 +183,31 @@ deltaFromDiff (T.lines -> old) (T.lines -> new) =
184
183
(V. fromList -> ! old2new, V. fromList -> ! new2old) = go diff 0 0
185
184
186
185
-- Compute previous and next lines that mapped successfully
187
- ! o2nPrevs = V. prescanl' fromMaybe (- 1 ) old2new
188
- ! o2nNexts = V. prescanr' (flip fromMaybe ) lnew old2new
186
+ ! o2nPrevs = V. prescanl' f (- 1 ) old2new
187
+ ! o2nNexts = V. prescanr' (flip f ) lnew old2new
189
188
190
- ! n2oPrevs = V. prescanl' fromMaybe (- 1 ) new2old
191
- ! n2oNexts = V. prescanr' (flip fromMaybe ) lold new2old
189
+ ! n2oPrevs = V. prescanl' f (- 1 ) new2old
190
+ ! n2oNexts = V. prescanr' (flip f ) lold new2old
192
191
193
- lookupPos :: Int -> V. Vector Int -> V. Vector Int -> V. Vector (Maybe Int ) -> Position -> PositionResult Position
192
+ f :: Int -> Int -> Int
193
+ f ! a ! b = if b == - 1 then a else b
194
+
195
+ lookupPos :: Int -> V. Vector Int -> V. Vector Int -> V. Vector Int -> Position -> PositionResult Position
194
196
lookupPos end prevs nexts xs (Position line col)
195
197
| line < 0 = PositionRange (Position 0 0 ) (Position 0 0 )
196
198
| line >= V. length xs = PositionRange (Position end 0 ) (Position end 0 )
197
199
| otherwise = case V. unsafeIndex xs line of
198
- Just line' -> PositionExact (Position line' col)
199
- Nothing ->
200
+ - 1 ->
200
201
-- look for the previous and next lines that mapped successfully
201
- let ! prev = 1 + (prevs V. ! line)
202
- ! next = nexts V. ! line
202
+ let ! prev = 1 + (V. unsafeIndex prevs line)
203
+ ! next = V. unsafeIndex nexts line
203
204
in PositionRange (Position prev 0 ) (Position next 0 )
205
+ line' -> PositionExact (Position line' col)
204
206
205
207
-- Construct a mapping between lines in the diff
206
- go :: [Diff T. Text ] -> Int -> Int -> ([Maybe Int ], [Maybe Int ])
208
+ -- -1 for unsucessful mapping
209
+ go :: [Diff T. Text ] -> Int -> Int -> ([Int ], [Int ])
207
210
go [] _ _ = ([] ,[] )
208
- go (Both _ _ : xs) ! lold ! lnew = bimap (Just lnew : ) (Just lold : ) $ go xs (lold+ 1 ) (lnew+ 1 )
209
- go (First _ : xs) ! lold ! lnew = first (Nothing : ) $ go xs (lold+ 1 ) lnew
210
- go (Second _ : xs) ! lold ! lnew = second (Nothing : ) $ go xs lold (lnew+ 1 )
211
-
212
-
211
+ go (Both _ _ : xs) ! lold ! lnew = bimap (lnew : ) (lold : ) $ go xs (lold+ 1 ) (lnew+ 1 )
212
+ go (First _ : xs) ! lold ! lnew = first (- 1 : ) $ go xs (lold+ 1 ) lnew
213
+ go (Second _ : xs) ! lold ! lnew = second (- 1 : ) $ go xs lold (lnew+ 1 )
0 commit comments