-
-
Notifications
You must be signed in to change notification settings - Fork 414
Fix notes plugin: avoid crashes on missing note definitions and improve lookup handling #4773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SATVIKsynopsis
wants to merge
2
commits into
haskell:master
Choose a base branch
from
SATVIKsynopsis:fix/notes-plugin-safe-lookup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| module Ide.Plugin.Notes (descriptor, Log) where | ||
|
|
||
| import Control.Lens ((^.)) | ||
| import Control.Monad.Except (ExceptT, MonadError, | ||
| throwError) | ||
| import Control.Monad.Except (ExceptT) | ||
| import Control.Monad.IO.Class (liftIO) | ||
| import qualified Data.Array as A | ||
| import Data.Foldable (foldl') | ||
|
|
@@ -11,13 +10,13 @@ import qualified Data.HashMap.Strict as HM | |
| import qualified Data.HashSet as HS | ||
| import Data.List (uncons) | ||
| import Data.Maybe (catMaybes, listToMaybe, | ||
| mapMaybe) | ||
| mapMaybe, fromMaybe) | ||
| import Data.Text (Text, intercalate) | ||
| import qualified Data.Text as T | ||
| import qualified Data.Text.Utf16.Rope.Mixed as Rope | ||
| import Data.Traversable (for) | ||
| import Development.IDE hiding (line) | ||
| import Development.IDE.Core.PluginUtils (runActionE, useE) | ||
|
|
||
| import Development.IDE.Core.Shake (toKnownFiles) | ||
| import qualified Development.IDE.Core.Shake as Shake | ||
| import Development.IDE.Graph.Classes (Hashable, NFData) | ||
|
|
@@ -63,11 +62,12 @@ type instance RuleResult GetNoteReferences = HashMap Text [(NormalizedFilePath, | |
|
|
||
| instance Pretty Log where | ||
| pretty = \case | ||
| LogShake l -> pretty l | ||
| LogNoteReferencesFound file refs -> "Found note references in " <> prettyNotes file refs | ||
| LogNotesFound file notes -> "Found notes in " <> prettyNotes file notes | ||
| where prettyNotes file hm = pretty (show file) <> ": [" | ||
| <> pretty (intercalate ", " (fmap (\(s, p) -> "\"" <> s <> "\" at " <> intercalate ", " (map (T.pack . show) p)) hm)) <> "]" | ||
| LogShake l -> pretty l | ||
| LogNoteReferencesFound file refs -> "Found note references in " <> prettyNotes file refs | ||
| LogNotesFound file notes -> "Found notes in " <> prettyNotes file notes | ||
| where | ||
| prettyNotes file hm = pretty (show file) <> ": [" | ||
| <> pretty (intercalate ", " (fmap (\(s, p) -> "\"" <> s <> "\" at " <> intercalate ", " (map (T.pack . show) p)) hm)) <> "]" | ||
|
|
||
| {- | ||
| The first time the user requests a jump-to-definition on a note reference, the | ||
|
|
@@ -100,25 +100,36 @@ findNotesRules recorder = do | |
| ) | ||
| pure $ Just $ foldl' (HM.unionWith (<>)) HM.empty definedReferences | ||
|
|
||
| err :: MonadError PluginError m => Text -> Maybe a -> m a | ||
| err s = maybe (throwError $ PluginInternalError s) pure | ||
|
|
||
| getNote :: NormalizedFilePath -> IdeState -> Position -> ExceptT PluginError (HandlerM c) (Maybe Text) | ||
| getNote nfp state (Position l c) = do | ||
| contents <- | ||
| err "Error getting file contents" | ||
| =<< liftIO (runAction "notes.getfileContents" state (getFileContents nfp)) | ||
| line <- err "Line not found in file" (listToMaybe $ Rope.lines $ fst | ||
| (Rope.splitAtLine 1 $ snd $ Rope.splitAtLine (fromIntegral l) contents)) | ||
| pure $ listToMaybe $ mapMaybe (atPos $ fromIntegral c) $ matchAllText noteRefRegex line | ||
| mContents <- liftIO (runAction "notes.getfileContents" state (getFileContents nfp)) | ||
| case mContents of | ||
| Nothing -> | ||
|
|
||
| pure Nothing | ||
|
|
||
| Just contents -> do | ||
|
|
||
| let ropeLine = snd $ Rope.splitAtLine (fromIntegral l) contents | ||
| mLine = listToMaybe $ Rope.lines $ fst (Rope.splitAtLine 1 ropeLine) | ||
|
|
||
| case mLine of | ||
| Nothing -> pure Nothing | ||
| Just ln -> | ||
|
|
||
| pure $ | ||
| listToMaybe $ | ||
| mapMaybe (atPos (fromIntegral c)) $ | ||
| matchAllText noteRefRegex ln | ||
| where | ||
| atPos c arr = case arr A.! 0 of | ||
| -- We check if the line we are currently at contains a note | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why delete this comment? |
||
| -- reference. However, we need to know if the cursor is within the | ||
| -- match or somewhere else. The second entry of the array contains | ||
| -- the title of the note as extracted by the regex. | ||
| (_, (c', len)) -> if c' <= c && c <= c' + len | ||
| then Just (fst (arr A.! 1)) else Nothing | ||
|
|
||
| atPos cur arr = | ||
| let (_, (start, len)) = arr A.! 0 | ||
| in if start <= cur && cur <= start + len | ||
| then Just (fst (arr A.! 1)) | ||
| else Nothing | ||
|
|
||
|
|
||
| listReferences :: PluginMethodHandler IdeState Method_TextDocumentReferences | ||
| listReferences state _ param | ||
|
|
@@ -127,32 +138,67 @@ listReferences state _ param | |
| let pos@(Position l _) = param ^. L.position | ||
| noteOpt <- getNote nfp state pos | ||
| case noteOpt of | ||
| Nothing -> pure (InR Null) | ||
| Nothing -> | ||
|
|
||
| pure (InR Null) | ||
|
|
||
| Just note -> do | ||
| notes <- runActionE "notes.definedNoteReferencess" state $ useE MkGetNoteReferences nfp | ||
| poss <- err ("Note reference (a comment of the form `{- Note [" <> note <> "] -}`) not found") (HM.lookup note notes) | ||
| pure $ InL (mapMaybe (\(noteFp, pos@(Position l' _)) -> if l' == l then Nothing else Just ( | ||
| Location (fromNormalizedUri $ normalizedFilePathToUri noteFp) (Range pos pos))) poss) | ||
| mNotes <- liftIO $ runAction "notes.definedNoteReferencess" state (use MkGetNoteReferences nfp) | ||
|
|
||
| let notes = fromMaybe HM.empty mNotes | ||
|
|
||
| case HM.lookup note notes of | ||
| Nothing -> | ||
|
|
||
| pure (InR Null) | ||
|
|
||
| Just poss -> | ||
| pure $ InL $ | ||
| mapMaybe | ||
| (\(noteFp, pos@(Position l' _)) -> | ||
| if l' == l | ||
| then Nothing | ||
| else Just (Location (fromNormalizedUri $ normalizedFilePathToUri noteFp) | ||
| (Range pos pos))) | ||
| poss | ||
| where | ||
| uriOrig = toNormalizedUri $ param ^. (L.textDocument . L.uri) | ||
| listReferences _ _ _ = throwError $ PluginInternalError "conversion to normalized file path failed" | ||
|
|
||
| listReferences _ _ _ = | ||
|
|
||
| pure (InR Null) | ||
|
|
||
|
|
||
| jumpToNote :: PluginMethodHandler IdeState Method_TextDocumentDefinition | ||
| jumpToNote state _ param | ||
| | Just nfp <- uriToNormalizedFilePath uriOrig | ||
| = do | ||
| noteOpt <- getNote nfp state (param ^. L.position) | ||
| case noteOpt of | ||
| Nothing -> pure (InR (InR Null)) | ||
| Nothing -> | ||
|
|
||
| pure (InR (InR Null)) | ||
|
|
||
| Just note -> do | ||
| notes <- runActionE "notes.definedNotes" state $ useE MkGetNotes nfp | ||
| (noteFp, pos) <- err ("Note definition (a comment of the form `{- Note [" <> note <> "]\\n~~~ ... -}`) not found") (HM.lookup note notes) | ||
| pure $ InL (Definition (InL | ||
| (Location (fromNormalizedUri $ normalizedFilePathToUri noteFp) (Range pos pos)) | ||
| )) | ||
| mNotes <- liftIO $ runAction "notes.definedNotes" state (use MkGetNotes nfp) | ||
|
|
||
| let notes = fromMaybe HM.empty mNotes | ||
|
|
||
| case HM.lookup note notes of | ||
| Nothing -> | ||
|
|
||
| pure (InR (InR Null)) | ||
|
|
||
| Just (noteFp, pos) -> | ||
| pure $ InL $ Definition $ InL | ||
| (Location (fromNormalizedUri $ normalizedFilePathToUri noteFp) (Range pos pos)) | ||
| where | ||
| uriOrig = toNormalizedUri $ param ^. (L.textDocument . L.uri) | ||
| jumpToNote _ _ _ = throwError $ PluginInternalError "conversion to normalized file path failed" | ||
|
|
||
| jumpToNote _ _ _ = | ||
|
|
||
| pure (InR (InR Null)) | ||
|
|
||
|
|
||
| findNotesInFile :: NormalizedFilePath -> Recorder (WithPriority Log) -> Action (Maybe (HM.HashMap Text Position, HM.HashMap Text [Position])) | ||
| findNotesInFile file recorder = do | ||
|
|
@@ -194,4 +240,4 @@ noteRefRegex, noteRegex :: Regex | |
| , mkReg ("note \\[([[:print:]]+)\\][[:blank:]]*\r?\n[[:blank:]]*(--)?[[:blank:]]*~~~" :: String) | ||
| ) | ||
| where | ||
| mkReg = makeRegexOpts (defaultCompOpt { caseSensitive = False }) defaultExecOpt | ||
| mkReg = makeRegexOpts (defaultCompOpt { caseSensitive = False }) defaultExecOpt | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the extra newlines?