module Development.IDE.Types.Location
( Location(..)
, noFilePath
, noRange
, Position(..)
, showPosition
, Range(..)
, Uri(..)
, NormalizedUri
, LSP.toNormalizedUri
, LSP.fromNormalizedUri
, NormalizedFilePath
, fromUri
, toNormalizedFilePath
, fromNormalizedFilePath
, filePathToUri
, filePathToUri'
, uriToFilePath'
) where
import Language.Haskell.LSP.Types (Location(..), Range(..), Position(..))
import Control.DeepSeq
import Data.Maybe as Maybe
import Data.Hashable
import Data.String
import System.FilePath
import System.Info.Extra
import qualified Language.Haskell.LSP.Types as LSP
import Language.Haskell.LSP.Types as LSP (
filePathToUri
, NormalizedUri(..)
, Uri(..)
, toNormalizedUri
, fromNormalizedUri
)
newtype NormalizedFilePath = NormalizedFilePath FilePath
deriving (Eq, Ord, Show, Hashable, NFData)
instance IsString NormalizedFilePath where
fromString = toNormalizedFilePath
toNormalizedFilePath :: FilePath -> NormalizedFilePath
toNormalizedFilePath "" = NormalizedFilePath ""
toNormalizedFilePath fp = NormalizedFilePath $ normalise' fp
where
normalise' :: FilePath -> FilePath
normalise' = oneSlash . map (\c -> if isPathSeparator c then pathSeparator else c)
oneSlash :: FilePath -> FilePath
oneSlash (x:xs) | isWindows = x : f xs
oneSlash xs = f xs
f (x:y:xs) | isPathSeparator x, isPathSeparator y = f (x:xs)
f (x:xs) = x : f xs
f [] = []
fromNormalizedFilePath :: NormalizedFilePath -> FilePath
fromNormalizedFilePath (NormalizedFilePath fp) = fp
uriToFilePath' :: Uri -> Maybe FilePath
uriToFilePath' uri
| uri == filePathToUri "" = Just ""
| otherwise = LSP.uriToFilePath uri
filePathToUri' :: NormalizedFilePath -> NormalizedUri
filePathToUri' = toNormalizedUri . filePathToUri . fromNormalizedFilePath
fromUri :: LSP.NormalizedUri -> NormalizedFilePath
fromUri = toNormalizedFilePath . fromMaybe noFilePath . uriToFilePath' . fromNormalizedUri
noFilePath :: FilePath
noFilePath = "<unknown>"
noRange :: Range
noRange = Range (Position 0 0) (Position 100000 0)
showPosition :: Position -> String
showPosition Position{..} = show (_line + 1) ++ ":" ++ show (_character + 1)