1-- Copyright (c) 2019 The DAML Authors. All rights reserved.
2-- SPDX-License-Identifier: Apache-2.0
3module Development.IDE.Core.PositionMapping
9 -- toCurrent and fromCurrent are mainly exposed for testing
15import qualified Data.Text as T
16import Language.Haskell.LSP.Types
18data PositionMapping = PositionMapping
19 { toCurrentPosition :: !(Position -> Maybe Position)
20 , fromCurrentPosition :: !(Position -> Maybe Position)
23toCurrentRange :: PositionMapping -> Range -> Maybe Range
24toCurrentRange mapping (Range a b) =
25 Range <$> toCurrentPosition mapping a <*> toCurrentPosition mapping b
27fromCurrentRange :: PositionMapping -> Range -> Maybe Range
28fromCurrentRange mapping (Range a b) =
29 Range <$> fromCurrentPosition mapping a <*> fromCurrentPosition mapping b
31idMapping :: PositionMapping
32idMapping = PositionMapping Just Just
34applyChange :: PositionMapping -> TextDocumentContentChangeEvent -> PositionMapping
35applyChange posMapping (TextDocumentContentChangeEvent (Just r) _ t) = PositionMapping
36 { toCurrentPosition = toCurrent r t <=< toCurrentPosition posMapping
37 , fromCurrentPosition = fromCurrentPosition posMapping <=< fromCurrent r t
39applyChange posMapping _ = posMapping
41toCurrent :: Range -> T.Text -> Position -> Maybe Position
42toCurrent (Range (Position startLine startColumn) (Position endLine endColumn)) t (Position line column)
43 | line < startLine || line == startLine && column < startColumn =
44 -- Position is before the change and thereby unchanged.
45 Just $ Position line column
46 | line > endLine || line == endLine && column >= endColumn =
47 -- Position is after the change so increase line and column number
49 Just $ Position (line + lineDiff) newColumn
51 -- Position is in the region that was changed.
53 lineDiff = linesNew - linesOld
54 linesNew = T.count "\n" t
55 linesOld = endLine - startLine
57 | linesNew == 0 = startColumn + T.length t
58 | otherwise = T.length $ T.takeWhileEnd (/= '\n') t
60 | line == endLine = column + newEndColumn - endColumn
63fromCurrent :: Range -> T.Text -> Position -> Maybe Position
64fromCurrent (Range (Position startLine startColumn) (Position endLine endColumn)) t (Position line column)
65 | line < startLine || line == startLine && column < startColumn =
66 -- Position is before the change and thereby unchanged
67 Just $ Position line column
68 | line > newEndLine || line == newEndLine && column >= newEndColumn =
69 -- Position is after the change so increase line and column number
71 Just $ Position (line - lineDiff) newColumn
73 -- Position is in the region that was changed.
75 lineDiff = linesNew - linesOld
76 linesNew = T.count "\n" t
77 linesOld = endLine - startLine
78 newEndLine = endLine + lineDiff
80 | linesNew == 0 = startColumn + T.length t
81 | otherwise = T.length $ T.takeWhileEnd (/= '\n') t
83 | line == newEndLine = column - (newEndColumn - endColumn)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list