on 210de4e help sign in
78 lines 2.3 kB history raw
1-- Copyright (c) 2019 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
2-- SPDX-License-Identifier: Apache-2.0
4{-# LANGUAGE OverloadedStrings #-}
6-- | Types and functions for working with source code locations.
7module Development.IDE.Types.Location
8 ( genLocation
9 , inRange
10 , inRangeClosed
11 , isGenLocation
12 , Location(..)
13 , appendLocation
14 , noLocation
15 , noFilePath
16 , noRange
17 , Position(..)
18 , Range(..)
19 , appendRange
20 ) where
22import Language.Haskell.LSP.Types (Location(..), Range(..), Position(..), Uri(..), filePathToUri)
24-- | A dummy location to use when location information is missing.
25noLocation :: Location
26noLocation = Location
27 { _uri = filePathToUri noFilePath
28 , _range = noRange
29 }
31noFilePath :: FilePath
32noFilePath = "<unknown>"
34-- A dummy range to use when range is unknown
35noRange :: Range
36noRange = Range (Position 0 0) (Position 100000 0)
39-- | A dummy location to use when location information is not present because
40-- the code was generated.
41genLocation :: Location
42genLocation = Location
43 { _uri = Uri "<generated>"
44 , _range = Range (Position 0 0) (Position 0 0)
45 }
48-- | Is a location generated.
49isGenLocation :: Location -> Bool
50isGenLocation x = _uri x == Uri "<generated>"
53-- | Check if a position is inside a range.
54-- Our definition states that the start of the range is included, but not the end.
55inRange :: Position -> Range -> Bool
56inRange pos (Range start end) = start <= pos && pos < end
59-- | Check if a position is inside a range, including the end.
60-- Both start and end of the range are included.
61inRangeClosed :: Position -> Range -> Bool
62inRangeClosed pos (Range start end) = start <= pos && pos <= end
65-- | Produce a new range where the minimum position is the min of both,
66-- and the maximum position is the max of both.
67appendRange :: Range -> Range -> Range
68appendRange r1 r2
69 = Range { _start = min (_start r1) (_start r2)
70 , _end = max (_end r1) (_end r2) }
73-- | Produce a new location where the ranges are the appended and we choose
74-- the file path of the second.
75appendLocation :: Location -> Location -> Location
76appendLocation l1 l2
77 = Location { _uri = _uri l2
78 , _range = appendRange (_range l1) (_range l2) }