on bbdcbdd help sign in
142 lines 4.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 , noRange
16 , Position(..)
17 , Range(..)
18 , appendRange
19 ) where
21import Control.DeepSeq (NFData (..))
22import Data.Aeson.Types (FromJSON, FromJSONKey, ToJSON, ToJSONKey)
23import Data.Binary (Binary)
24import Data.Data
25import Data.Text.Prettyprint.Doc.Syntax
26import GHC.Generics
28------------------------------------------------------------------------------
29--- Types
30------------------------------------------------------------------------------
32-- | Position in a text document expressed as zero-based line and
33-- character offset.
34data Position = Position
35 { positionLine :: {-# UNPACK #-} !Int
36 -- ^ Zero-based line position in the document.
37 , positionCharacter :: {-# UNPACK #-} !Int
38 -- ^ Zero-based character offset on the line.
39 } deriving (Eq, Ord, Read, Show, Generic, Data)
41instance NFData Position
43instance Pretty Position where
44 pretty pos =
45 pretty (positionLine pos + 1) <> colon <> pretty (positionCharacter pos + 1)
48-- | A range in a text document expressed as inclusive start-position and an
49-- exclusive end-position.
50data Range = Range
51 { rangeStart :: {-# UNPACK #-} !Position
52 -- ^ The start position of the range, which is considered to be part of
53 -- the range.
54 , rangeEnd :: {-# UNPACK #-} !Position
55 -- ^ The end position of the range, which is not considered to be part
56 -- of the range.
57 } deriving (Eq, Ord, Read, Show, Generic, Data)
59instance NFData Range
61instance Pretty Range where
62 pretty range =
63 pretty (rangeStart range) <> "-" <> pretty (rangeEnd range)
66-- | Represents a location inside a resource, such as a line inside a text file.
67data Location = Location
68 { lFilePath :: !FilePath
69 -- ^ The uri of the document.
70 , lRange :: !Range
71 -- ^ The range within the document.
72 } deriving (Eq, Ord, Read, Show, Generic, Data)
74instance NFData Location
77-- | A dummy location to use when location information is missing.
78noLocation :: Location
79noLocation = Location
80 { lFilePath = "<unknown>"
81 , lRange = noRange
82 }
84-- A dummy range to use when range is unknown
85noRange :: Range
86noRange = Range (Position 0 0) (Position 100000 0)
89-- | A dummy location to use when location information is not present because
90-- the code was generated.
91genLocation :: Location
92genLocation = Location
93 { lFilePath = "<generated>"
94 , lRange = Range (Position 0 0) (Position 0 0)
95 }
98-- | Is a location generated.
99isGenLocation :: Location -> Bool
100isGenLocation x = lFilePath x == "<generated>"
103-- | Check if a position is inside a range.
104-- Our definition states that the start of the range is included, but not the end.
105inRange :: Position -> Range -> Bool
106inRange pos (Range start end) = start <= pos && pos < end
109-- | Check if a position is inside a range, including the end.
110-- Both start and end of the range are included.
111inRangeClosed :: Position -> Range -> Bool
112inRangeClosed pos (Range start end) = start <= pos && pos <= end
115-- | Produce a new range where the minimum position is the min of both,
116-- and the maximum position is the max of both.
117appendRange :: Range -> Range -> Range
118appendRange r1 r2
119 = Range { rangeStart = min (rangeStart r1) (rangeStart r2)
120 , rangeEnd = max (rangeEnd r1) (rangeEnd r2) }
123-- | Produce a new location where the ranges are the appended and we choose
124-- the file path of the second.
125appendLocation :: Location -> Location -> Location
126appendLocation l1 l2
127 = Location { lFilePath = lFilePath l2
128 , lRange = appendRange (lRange l1) (lRange l2) }
130instance ToJSON Position
131instance FromJSON Position
132instance ToJSONKey Position
133instance FromJSONKey Position
134instance Binary Position
136instance ToJSON Range
137instance FromJSON Range
138instance Binary Range
140instance ToJSON Location
141instance FromJSON Location
142instance Binary Location