@@ -12,89 +12,41 @@ module Development.IDE.LSP.Notifications
1212import Development.IDE.LSP.Protocol
1313import Development.IDE.LSP.Server hiding (runServer)
1414import qualified Language.Haskell.LSP.Core as LSP
15+import qualified Language.Haskell.LSP.Types as LSP
1516
1617import Development.IDE.Types.Logger
1718import Development.IDE.Core.Service
1819import Development.IDE.Types.Location
1920
2021import qualified Data.Set as S
21−import qualified Data.Text as T
2222
2323import Development.IDE.Core.FileStore
2424import Development.IDE.Core.OfInterest
2525
26−import qualified Network.URI as URI
27−
28−
29−textShow :: Show a => a -> T.Text
30−textShow = T.pack . show
31−
32−
33−setHandlersNotifications :: WithMessage -> LSP.Handlers -> IO LSP.Handlers
34−setHandlersNotifications WithMessage{..} x = return x{
35− LSP.didOpenTextDocumentNotificationHandler = withNotification $ \ide (DidOpenTextDocumentParams item) -> do
36− case URI.parseURI $ T.unpack $ getUri $ _uri (item :: TextDocumentItem) of
37− Just uri
38− | URI.uriScheme uri == "file:"
39− -> handleDidOpenFile ide item
40−
41− | otherwise
42− -> logWarning (ideLogger ide) $ "Unknown scheme in URI: "
43− <> textShow uri
44−
45− _ -> logSeriousError (ideLogger ide) $ "Invalid URI in DidOpenTextDocument: "
46− <> textShow (_uri (item :: TextDocumentItem))
47−
48− ,LSP.didChangeTextDocumentNotificationHandler = withNotification $ \ide (DidChangeTextDocumentParams docId _) -> do
49− let uri = _uri (docId :: VersionedTextDocumentIdentifier)
50−
51− case uriToFilePath' uri of
52− Just (toNormalizedFilePath -> filePath) -> do
53− onFileModified ide filePath
54− logInfo (ideLogger ide)
55− $ "Updated text document: " <> textShow (fromNormalizedFilePath filePath)
56−
57− Nothing ->
58− logSeriousError (ideLogger ide)
59− $ "Invalid file path: " <> textShow (_uri (docId :: VersionedTextDocumentIdentifier))
60−
61− ,LSP.didCloseTextDocumentNotificationHandler = withNotification $ \ide (DidCloseTextDocumentParams (TextDocumentIdentifier uri)) ->
62− case URI.parseURI $ T.unpack $ getUri uri of
63− Just uri'
64− | URI.uriScheme uri' == "file:" -> do
65− Just fp <- pure $ toNormalizedFilePath <$> uriToFilePath' uri
66− handleDidCloseFile ide fp
67− | otherwise -> logWarning (ideLogger ide) $ "Unknown scheme in URI: " <> textShow uri
68−
69− _ -> logSeriousError (ideLogger ide)
70− $ "Invalid URI in DidCloseTextDocument: "
71− <> textShow uri
7226
27+whenUriFile :: IdeState -> Uri -> (NormalizedFilePath -> IO ()) -> IO ()
28+whenUriFile ide uri act = case LSP.uriToFilePath uri of
29+ Just file -> act $ toNormalizedFilePath file
30+ Nothing -> logWarning (ideLogger ide) $ "Unknown scheme in URI: " <> getUri uri
31+
32+setHandlersNotifications :: PartialHandlers
33+setHandlersNotifications = PartialHandlers $ \WithMessage{..} x -> return x
34+ {LSP.didOpenTextDocumentNotificationHandler = withNotification (LSP.didOpenTextDocumentNotificationHandler x) $
35+ \ide (DidOpenTextDocumentParams TextDocumentItem{_uri}) -> do
36+ setSomethingModified ide
37+ whenUriFile ide _uri $ \file ->
38+ modifyFilesOfInterest ide (S.insert file)
39+ logInfo (ideLogger ide) $ "Opened text document: " <> getUri _uri
40+
41+ ,LSP.didChangeTextDocumentNotificationHandler = withNotification (LSP.didChangeTextDocumentNotificationHandler x) $
42+ \ide (DidChangeTextDocumentParams VersionedTextDocumentIdentifier{_uri} _) -> do
43+ setSomethingModified ide
44+ logInfo (ideLogger ide) $ "Modified text document: " <> getUri _uri
45+
46+ ,LSP.didCloseTextDocumentNotificationHandler = withNotification (LSP.didCloseTextDocumentNotificationHandler x) $
47+ \ide (DidCloseTextDocumentParams TextDocumentIdentifier{_uri}) -> do
48+ setSomethingModified ide
49+ whenUriFile ide _uri $ \file ->
50+ modifyFilesOfInterest ide (S.delete file)
51+ logInfo (ideLogger ide) $ "Closed text document: " <> getUri _uri
7352 }
74− where
75− -- Note that the state changes here are not atomic.
76− -- When we have parallel compilation we could manage the state
77− -- changes in STM so that we can atomically change the state.
78− -- Internally it should be done via the IO oracle. See PROD-2808.
79− handleDidOpenFile ide (TextDocumentItem uri _ _ _) = do
80− Just filePath <- pure $ toNormalizedFilePath <$> uriToFilePath' uri
81− onFileModified ide filePath
82− modifyFilesOfInterest ide (S.insert filePath)
83− logInfo (ideLogger ide) $ "Opened text document: " <> textShow filePath
84−
85− handleDidCloseFile ide filePath = do
86− logInfo (ideLogger ide) $ "Closed text document: " <> textShow (fromNormalizedFilePath filePath)
87− onFileModified ide filePath
88− modifyFilesOfInterest ide (S.delete filePath)
89−
90−
91−-- | Manages the file store (caching compilation results and unsaved content).
92−onFileModified
93− :: IdeState
94− -> NormalizedFilePath
95− -> IO ()
96−onFileModified service fp = do
97− logDebug (ideLogger service) $ "File modified " <> T.pack (show fp)
98− -- if we get here then we must be using the LSP framework, in which case we don't
99− -- need to bother sending file modifications, other than to force the database to rerun
100− setBufferModified service fp Nothing