+-- | /Creates/ a new text document. This is different from 'openDoc'
+-- as it sends a workspace/didChangeWatchedFiles notification letting the server
+-- know that a file was created within the workspace, __provided that the server
+-- has registered for it__, and the file matches any patterns the server
+-- registered for.
+-- It /does not/ actually create a file on disk, but is useful for convincing
+-- the server that one does exist.
+--
+-- @since 11.0.0.0
+createDoc :: FilePath -- ^ The path to the document to open, __relative to the root directory__.
+ -> String -- ^ The text document's language identifier, e.g. @"haskell"@.
+ -> T.Text -- ^ The content of the text document to create.
+ -> Session TextDocumentIdentifier -- ^ The identifier of the document just created.
+createDoc file languageId contents = do
+ dynCaps <- curDynCaps <$> get
+ rootDir <- asks rootDir
+ caps <- asks sessionCapabilities
+ absFile <- liftIO $ canonicalizePath (rootDir </> file)
+ let regs = filter (\r -> r ^. method == WorkspaceDidChangeWatchedFiles) $
+ Map.elems dynCaps
+ watchHits :: FileSystemWatcher -> Bool
+ watchHits (FileSystemWatcher pattern kind) =
+ -- If WatchKind is exlcuded, defaults to all true as per spec
+ fileMatches pattern && createHits (fromMaybe (WatchKind True True True) kind)
+
+ fileMatches pattern = Glob.match (Glob.compile pattern) relOrAbs
+ -- If the pattern is absolute then match against the absolute fp
+ where relOrAbs
+ | isAbsolute pattern = absFile
+ | otherwise = file
+
+ createHits (WatchKind create _ _) = create
+
+ regHits :: Registration -> Bool
+ regHits reg = isJust $ do
+ opts <- reg ^. registerOptions
+ fileWatchOpts <- case fromJSON opts :: Result DidChangeWatchedFilesRegistrationOptions of
+ Success x -> Just x
+ Error _ -> Nothing
+ if foldl' (\acc w -> acc || watchHits w) False (fileWatchOpts ^. watchers)
+ then Just ()
+ else Nothing
+
+ clientCapsSupports =
+ caps ^? workspace . _Just . didChangeWatchedFiles . _Just . dynamicRegistration . _Just
+ == Just True
+ shouldSend = clientCapsSupports && foldl' (\acc r -> acc || regHits r) False regs
+
+ when shouldSend $
+ sendNotification WorkspaceDidChangeWatchedFiles $ DidChangeWatchedFilesParams $
+ List [ FileEvent (filePathToUri file) FcCreated ]
+ openDoc' file languageId contents
+
+-- | Opens a text document that /exists on disk/, and sends a
+-- textDocument/didOpen notification to the server.