Skip to content

Commit d8178bd

Browse files
committed
Unboxed vector for position mapping
1 parent 7d66e3d commit d8178bd

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

ghcide/src/Development/IDE/Core/PositionMapping.hs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ import qualified Data.Text as T
2727
import Language.Haskell.LSP.Types
2828
import Data.List
2929
import Data.Algorithm.Diff
30-
import Data.Maybe
3130
import Data.Bifunctor
3231
import Control.DeepSeq
33-
import qualified Data.Vector as V
32+
import qualified Data.Vector.Unboxed as V
3433

3534
-- | Either an exact position, or the range of text that was substituted
3635
data PositionResult a
@@ -184,29 +183,31 @@ deltaFromDiff (T.lines -> old) (T.lines -> new) =
184183
(V.fromList -> !old2new, V.fromList -> !new2old) = go diff 0 0
185184

186185
-- 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
189188

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
192191

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
194196
lookupPos end prevs nexts xs (Position line col)
195197
| line < 0 = PositionRange (Position 0 0) (Position 0 0)
196198
| line >= V.length xs = PositionRange (Position end 0) (Position end 0)
197199
| otherwise = case V.unsafeIndex xs line of
198-
Just line' -> PositionExact (Position line' col)
199-
Nothing ->
200+
-1 ->
200201
-- 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
203204
in PositionRange (Position prev 0) (Position next 0)
205+
line' -> PositionExact (Position line' col)
204206

205207
-- 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])
207210
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)

ghcide/src/Development/IDE/Core/Service.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ import qualified Language.Haskell.LSP.Types.Capabilities as LSP
3232

3333
import Development.IDE.Core.Shake
3434
import Control.Monad
35-
import HieDb.Types
36-
3735

3836

3937
------------------------------------------------------------

0 commit comments

Comments
 (0)