1
1
mirror of https://github.com/github/semantic.git synced 2024-12-18 04:11:48 +03:00

Define a custom ToTagBy instance for MethodDeclaration.

This commit is contained in:
Rob Rix 2019-09-27 18:35:01 -04:00
parent 2921c4ec73
commit a2cf763f3f
No known key found for this signature in database
GPG Key ID: F188A01508EA1CF7

View File

@ -6,9 +6,7 @@ module Language.Java.Tags
import Control.Effect.Reader import Control.Effect.Reader
import Control.Effect.Writer import Control.Effect.Writer
import Data.Foldable (traverse_) import Data.Foldable (traverse_)
import Data.Maybe (listToMaybe)
import Data.Monoid (Ap(..)) import Data.Monoid (Ap(..))
import Data.List.NonEmpty (NonEmpty(..))
import Data.Text as Text import Data.Text as Text
import GHC.Generics import GHC.Generics
import Source.Loc import Source.Loc
@ -45,6 +43,7 @@ data Strategy = Generic | Custom
type family ToTagsInstance t :: Strategy where type family ToTagsInstance t :: Strategy where
ToTagsInstance (_ :+: _) = 'Custom ToTagsInstance (_ :+: _) = 'Custom
ToTagsInstance Java.MethodDeclaration = 'Custom
ToTagsInstance _ = 'Generic ToTagsInstance _ = 'Generic
@ -52,6 +51,31 @@ instance (ToTags l, ToTags r) => ToTagsBy 'Custom (l :+: r) where
tags' (L1 l) = tags l tags' (L1 l) = tags l
tags' (R1 r) = tags r tags' (R1 r) = tags r
instance ToTagsBy 'Custom Java.MethodDeclaration where
tags' Java.MethodDeclaration
{ ann = Loc range span
, name = Java.Identifier { bytes = name }
, dimensions
, extraChildren
, typeParameters
, parameters
, type'
, body
} = do
src <- ask @Source
let sliced = slice src range
{ end = case body of
Just Java.Block { ann = Loc Range { end } _ } -> end
Nothing -> end range
}
Tags.yield (Tag name Function span (firstLine sliced) Nothing)
traverse_ tags typeParameters
tags parameters
tags type'
traverse_ tags dimensions
traverse_ tags extraChildren
traverse_ tags body
firstLine :: Source -> Text firstLine :: Source -> Text
firstLine = Text.takeWhile (/= '\n') . toText . Source.take 180 firstLine = Text.takeWhile (/= '\n') . toText . Source.take 180