-- | Generally useful definition used in various places in the
-- language server implementation.
module Futhark.LSP.Tool
  ( getHoverInfoFromState,
    findDefinitionRange,
    rangeFromSrcLoc,
    rangeFromLoc,
    posToUri,
    computeMapping,
  )
where

import Data.Text qualified as T
import Futhark.Compiler.Program (lpImports)
import Futhark.LSP.PositionMapping
  ( PositionMapping,
    mappingFromDiff,
    toCurrentLoc,
    toStalePos,
  )
import Futhark.LSP.State (State (..), getStaleContent, getStaleMapping)
import Futhark.Util.Loc (Loc (Loc, NoLoc), Pos (Pos), SrcLoc, locOf)
import Futhark.Util.Pretty (prettyText)
import Language.Futhark.Prop (isBuiltinLoc)
import Language.Futhark.Query
  ( AtPos (AtName),
    BoundTo (..),
    atPos,
    boundLoc,
  )
import Language.LSP.Server (LspM, getVirtualFile)
import Language.LSP.Types
import Language.LSP.VFS (VirtualFile, virtualFileText, virtualFileVersion)

-- | Retrieve hover info for the definition referenced at the given
-- file at the given line and column number (the two 'Int's).
getHoverInfoFromState :: State -> Maybe FilePath -> Int -> Int -> Maybe Hover
getHoverInfoFromState :: State -> Maybe FilePath -> Int -> Int -> Maybe Hover
getHoverInfoFromState State
state (Just FilePath
path) Int
l Int
c = do
  AtName QualName VName
_ (Just BoundTo
def) Loc
loc <- State -> Pos -> Maybe AtPos
queryAtPos State
state forall a b. (a -> b) -> a -> b
$ FilePath -> Int -> Int -> Int -> Pos
Pos FilePath
path Int
l Int
c Int
0
  let msg :: Text
msg =
        case BoundTo
def of
          BoundTerm StructType
t Loc
_ -> forall a. Pretty a => a -> Text
prettyText StructType
t
          BoundModule {} -> Text
"module"
          BoundModuleType {} -> Text
"module type"
          BoundType {} -> Text
"type"
      ms :: HoverContents
ms = MarkupContent -> HoverContents
HoverContents forall a b. (a -> b) -> a -> b
$ MarkupKind -> Text -> MarkupContent
MarkupContent MarkupKind
MkPlainText Text
msg
  forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ HoverContents -> Maybe Range -> Hover
Hover HoverContents
ms (forall a. a -> Maybe a
Just (Loc -> Range
rangeFromLoc Loc
loc))
getHoverInfoFromState State
_ Maybe FilePath
_ Int
_ Int
_ = forall a. Maybe a
Nothing

-- | Find the location of the definition referenced at the given file
-- at the given line and column number (the two 'Int's).
findDefinitionRange :: State -> Maybe FilePath -> Int -> Int -> Maybe Location
findDefinitionRange :: State -> Maybe FilePath -> Int -> Int -> Maybe Location
findDefinitionRange State
state (Just FilePath
path) Int
l Int
c = do
  -- some unnessecary operations inside `queryAtPos` for this function
  -- but shouldn't affect performance much since "Go to definition" is called less frequently
  AtName QualName VName
_qn (Just BoundTo
bound) Loc
_loc <- State -> Pos -> Maybe AtPos
queryAtPos State
state forall a b. (a -> b) -> a -> b
$ FilePath -> Int -> Int -> Int -> Pos
Pos FilePath
path Int
l Int
c Int
0
  let loc :: Loc
loc = BoundTo -> Loc
boundLoc BoundTo
bound
      Loc (Pos FilePath
file_path Int
_ Int
_ Int
_) Pos
_ = Loc
loc
  if forall a. Located a => a -> Bool
isBuiltinLoc Loc
loc
    then forall a. Maybe a
Nothing
    else forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ Uri -> Range -> Location
Location (FilePath -> Uri
filePathToUri FilePath
file_path) (Loc -> Range
rangeFromLoc Loc
loc)
findDefinitionRange State
_ Maybe FilePath
_ Int
_ Int
_ = forall a. Maybe a
Nothing

-- | Query the AST for information at certain Pos.
queryAtPos :: State -> Pos -> Maybe AtPos
queryAtPos :: State -> Pos -> Maybe AtPos
queryAtPos State
state Pos
pos = do
  let Pos FilePath
path Int
_ Int
_ Int
_ = Pos
pos
      mapping :: Maybe PositionMapping
mapping = State -> FilePath -> Maybe PositionMapping
getStaleMapping State
state FilePath
path
  LoadedProg
loaded_prog <- State -> Maybe LoadedProg
stateProgram State
state
  Pos
stale_pos <- Maybe PositionMapping -> Pos -> Maybe Pos
toStalePos Maybe PositionMapping
mapping Pos
pos
  AtPos
query_result <- Imports -> Pos -> Maybe AtPos
atPos (LoadedProg -> Imports
lpImports LoadedProg
loaded_prog) Pos
stale_pos
  Maybe PositionMapping -> AtPos -> Maybe AtPos
updateAtPos Maybe PositionMapping
mapping AtPos
query_result
  where
    -- Update the 'AtPos' with the current mapping.
    updateAtPos :: Maybe PositionMapping -> AtPos -> Maybe AtPos
    updateAtPos :: Maybe PositionMapping -> AtPos -> Maybe AtPos
updateAtPos Maybe PositionMapping
mapping (AtName QualName VName
qn (Just BoundTo
def) Loc
loc) = do
      let def_loc :: Loc
def_loc = BoundTo -> Loc
boundLoc BoundTo
def
          Loc (Pos FilePath
def_file Int
_ Int
_ Int
_) Pos
_ = Loc
def_loc
          Pos FilePath
current_file Int
_ Int
_ Int
_ = Pos
pos
      Loc
current_loc <- Maybe PositionMapping -> Loc -> Maybe Loc
toCurrentLoc Maybe PositionMapping
mapping Loc
loc
      if FilePath
def_file forall a. Eq a => a -> a -> Bool
== FilePath
current_file
        then do
          Loc
current_def_loc <- Maybe PositionMapping -> Loc -> Maybe Loc
toCurrentLoc Maybe PositionMapping
mapping Loc
def_loc
          forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ QualName VName -> Maybe BoundTo -> Loc -> AtPos
AtName QualName VName
qn (forall a. a -> Maybe a
Just (BoundTo -> Loc -> BoundTo
updateBoundLoc BoundTo
def Loc
current_def_loc)) Loc
current_loc
        else do
          -- Defined in another file, get the corresponding PositionMapping.
          let def_mapping :: Maybe PositionMapping
def_mapping = State -> FilePath -> Maybe PositionMapping
getStaleMapping State
state FilePath
def_file
          Loc
current_def_loc <- Maybe PositionMapping -> Loc -> Maybe Loc
toCurrentLoc Maybe PositionMapping
def_mapping Loc
def_loc
          forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ QualName VName -> Maybe BoundTo -> Loc -> AtPos
AtName QualName VName
qn (forall a. a -> Maybe a
Just (BoundTo -> Loc -> BoundTo
updateBoundLoc BoundTo
def Loc
current_def_loc)) Loc
current_loc
    updateAtPos Maybe PositionMapping
_ AtPos
_ = forall a. Maybe a
Nothing

    updateBoundLoc :: BoundTo -> Loc -> BoundTo
    updateBoundLoc :: BoundTo -> Loc -> BoundTo
updateBoundLoc (BoundTerm StructType
t Loc
_loc) Loc
current_loc = StructType -> Loc -> BoundTo
BoundTerm StructType
t Loc
current_loc
    updateBoundLoc (BoundModule Loc
_loc) Loc
current_loc = Loc -> BoundTo
BoundModule Loc
current_loc
    updateBoundLoc (BoundModuleType Loc
_loc) Loc
current_loc = Loc -> BoundTo
BoundModuleType Loc
current_loc
    updateBoundLoc (BoundType Loc
_loc) Loc
current_loc = Loc -> BoundTo
BoundType Loc
current_loc

-- | Entry point for computing PositionMapping.
computeMapping :: State -> Maybe FilePath -> LspM () (Maybe PositionMapping)
computeMapping :: State -> Maybe FilePath -> LspM () (Maybe PositionMapping)
computeMapping State
state (Just FilePath
file_path) = do
  Maybe VirtualFile
virtual_file <- forall config (m :: * -> *).
MonadLsp config m =>
NormalizedUri -> m (Maybe VirtualFile)
getVirtualFile forall a b. (a -> b) -> a -> b
$ Uri -> NormalizedUri
toNormalizedUri forall a b. (a -> b) -> a -> b
$ FilePath -> Uri
filePathToUri FilePath
file_path
  forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ Maybe VirtualFile -> Maybe VirtualFile -> Maybe PositionMapping
getMapping (State -> FilePath -> Maybe VirtualFile
getStaleContent State
state FilePath
file_path) Maybe VirtualFile
virtual_file
  where
    getMapping :: Maybe VirtualFile -> Maybe VirtualFile -> Maybe PositionMapping
    getMapping :: Maybe VirtualFile -> Maybe VirtualFile -> Maybe PositionMapping
getMapping (Just VirtualFile
stale_file) (Just VirtualFile
current_file) =
      if VirtualFile -> Int32
virtualFileVersion VirtualFile
stale_file forall a. Eq a => a -> a -> Bool
== VirtualFile -> Int32
virtualFileVersion VirtualFile
current_file
        then forall a. Maybe a
Nothing -- Happens when other files (e.g. dependencies) fail to type-check.
        else forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ [Text] -> [Text] -> PositionMapping
mappingFromDiff (Text -> [Text]
T.lines forall a b. (a -> b) -> a -> b
$ VirtualFile -> Text
virtualFileText VirtualFile
stale_file) (Text -> [Text]
T.lines forall a b. (a -> b) -> a -> b
$ VirtualFile -> Text
virtualFileText VirtualFile
current_file)
    getMapping Maybe VirtualFile
_ Maybe VirtualFile
_ = forall a. Maybe a
Nothing
computeMapping State
_ Maybe FilePath
_ = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing

-- | Convert a Futhark 'Pos' to an LSP 'Uri'.
posToUri :: Pos -> Uri
posToUri :: Pos -> Uri
posToUri (Pos FilePath
file Int
_ Int
_ Int
_) = FilePath -> Uri
filePathToUri FilePath
file

-- Futhark's parser has a slightly different notion of locations than
-- LSP; so we tweak the positions here.
getStartPos :: Pos -> Position
getStartPos :: Pos -> Position
getStartPos (Pos FilePath
_ Int
l Int
c Int
_) =
  UInt -> UInt -> Position
Position (forall a. Enum a => Int -> a
toEnum Int
l forall a. Num a => a -> a -> a
- UInt
1) (forall a. Enum a => Int -> a
toEnum Int
c forall a. Num a => a -> a -> a
- UInt
1)

getEndPos :: Pos -> Position
getEndPos :: Pos -> Position
getEndPos (Pos FilePath
_ Int
l Int
c Int
_) =
  UInt -> UInt -> Position
Position (forall a. Enum a => Int -> a
toEnum Int
l forall a. Num a => a -> a -> a
- UInt
1) (forall a. Enum a => Int -> a
toEnum Int
c)

-- | Create an LSP 'Range' from a Futhark 'Loc'.
rangeFromLoc :: Loc -> Range
rangeFromLoc :: Loc -> Range
rangeFromLoc (Loc Pos
start Pos
end) = Position -> Position -> Range
Range (Pos -> Position
getStartPos Pos
start) (Pos -> Position
getEndPos Pos
end)
rangeFromLoc Loc
NoLoc = Position -> Position -> Range
Range (UInt -> UInt -> Position
Position UInt
0 UInt
0) (UInt -> UInt -> Position
Position UInt
0 UInt
5) -- only when file not found, throw error after moving to vfs

-- | Create an LSP 'Range' from a Futhark 'SrcLoc'.
rangeFromSrcLoc :: SrcLoc -> Range
rangeFromSrcLoc :: SrcLoc -> Range
rangeFromSrcLoc = Loc -> Range
rangeFromLoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Located a => a -> Loc
locOf