haskell / lsp

Haskell library for the Microsoft Language Server Protocol
360 stars 89 forks source link

`getVirtualFile` always returns `Nothing` #507

Closed aabounegm closed 11 months ago

aabounegm commented 11 months ago

I'm trying to implement a language server with the latest versions of this package (lsp-2.1.0.0 and lsp-types-2.0.1.0), but I can't seem to be able to get the content of any file to analyze it. I followed the Reactor example (the SMethod_TextDocumentDidChange handler in particular), but getVirtualFile returns Nothing. My handler looks like so:

requestHandler SMethod_TextDocumentSemanticTokensFull $ \req responder -> do
        let doc = req ^. params . textDocument . uri . to toNormalizedUri
        mdoc <- getVirtualFile doc
        case virtualFileText <$> mdoc of
          Nothing -> do
            let (NormalizedUri _ path) = doc
            _ <- sendNotification SMethod_WindowShowMessage (ShowMessageParams MessageType_Error (T.concat ["Couldn't open file: ", path]))
            return ()
          Just sourceCode -> do
            _ <- sendNotification SMethod_WindowShowMessage (ShowMessageParams MessageType_Info "File opened")
            -- TODO: tokenize `sourceCode` and send with `responder`
            return ()

but it always sends the error notification: image

I tested on Windows 11 and macOS, but no difference.

Full (but minimal) code can be found here. The hover handler works correctly, so I'm sure the server is running and communicates with the extension properly. For reference, the extension code can be found here.

michaelpj commented 11 months ago

If you look at the message logs, is your client telling the server about those files? https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_synchronization

The VFS reflects what the client tells it the state of files is, and nothing else.

aabounegm commented 11 months ago

You are right, thank you! I didn't add syncOptions before (I didn't understand what it's for), but after adding it the handler now works properly. Such a silly overlook on my behalf 😅, I will close the issue now. For future reference, can you point me to any tutorial or good documentation for implementing an LSP server with this package specifically (most of the tutorials out there are for Node.js), preferably somewhat beginner-friendly?

michaelpj commented 11 months ago

Sorry, I don't know of any tutorials for using this library. You could look at some of the projects using it: HLS (very complex), static-ls (https://github.com/josephsumabat/static-ls), dhall (https://github.com/dhall-lang/dhall-haskell).