From 57f284f847458679e06f52e443e834aabaee4d00 Mon Sep 17 00:00:00 2001 From: Timothy Clem Date: Thu, 8 Nov 2018 08:33:26 -0800 Subject: [PATCH] Streamline the declarations for toc summaries --- semantic.cabal | 2 +- .../{Declaration.hs => TOCSummary.hs} | 41 ++----------------- src/Rendering/TOC.hs | 12 +----- src/Semantic/Diff.hs | 2 +- test/Data/Functor/Listable.hs | 2 +- test/Rendering/TOC/Spec.hs | 2 +- 6 files changed, 10 insertions(+), 51 deletions(-) rename src/Analysis/{Declaration.hs => TOCSummary.hs} (82%) diff --git a/semantic.cabal b/semantic.cabal index 4470054c9..49320ad51 100644 --- a/semantic.cabal +++ b/semantic.cabal @@ -27,7 +27,7 @@ library , Analysis.Abstract.Tracing , Analysis.ConstructorName , Analysis.CyclomaticComplexity - , Analysis.Declaration + , Analysis.TOCSummary , Analysis.Decorator , Analysis.HasTextElement , Analysis.PackageDef diff --git a/src/Analysis/Declaration.hs b/src/Analysis/TOCSummary.hs similarity index 82% rename from src/Analysis/Declaration.hs rename to src/Analysis/TOCSummary.hs index 178a8176f..e48a43e8d 100644 --- a/src/Analysis/Declaration.hs +++ b/src/Analysis/TOCSummary.hs @@ -1,15 +1,13 @@ {-# LANGUAGE RankNTypes, ScopedTypeVariables, TypeFamilies, TypeOperators, UndecidableInstances #-} -module Analysis.Declaration +module Analysis.TOCSummary ( Declaration(..) , HasDeclaration , declarationAlgebra ) where -import Prologue hiding (first, project) - -import Control.Arrow hiding (first) -import qualified Data.Text as T +import Prologue hiding (project) +import Control.Arrow import Control.Rewriting hiding (apply) import Data.Blob import Data.Error (Error (..), showExpectation) @@ -20,15 +18,12 @@ import Data.Source as Source import qualified Data.Syntax as Syntax import qualified Data.Syntax.Declaration as Declaration import Data.Term +import qualified Data.Text as T import qualified Language.Markdown.Syntax as Markdown -import qualified Language.Ruby.Syntax as Ruby.Syntax -import qualified Language.TypeScript.Syntax as TypeScript.Syntax -- | A declaration’s identifier and type. data Declaration = MethodDeclaration { declarationIdentifier :: Text, declarationText :: Text, declarationSpan :: Span, declarationLanguage :: Language, declarationReceiver :: Maybe Text } - | ClassDeclaration { declarationIdentifier :: Text, declarationText :: Text, declarationSpan :: Span, declarationLanguage :: Language } - | ModuleDeclaration { declarationIdentifier :: Text, declarationText :: Text, declarationSpan :: Span, declarationLanguage :: Language } | FunctionDeclaration { declarationIdentifier :: Text, declarationText :: Text, declarationSpan :: Span, declarationLanguage :: Language } | HeadingDeclaration { declarationIdentifier :: Text, declarationText :: Text, declarationSpan :: Span, declarationLanguage :: Language, declarationLevel :: Int } | ErrorDeclaration { declarationIdentifier :: Text, declarationText :: Text, declarationSpan :: Span, declarationLanguage :: Language } @@ -117,27 +112,6 @@ instance CustomHasDeclaration whole Declaration.Method where isEmpty = (== 0) . rangeLength . locationByteRange methodSource = getIdentifier (arr Declaration.methodBody) blob (In ann decl) --- | Produce a 'ClassDeclaration' for 'Declaration.Class' nodes. -instance CustomHasDeclaration whole Declaration.Class where - customToDeclaration blob@Blob{..} ann decl@(Declaration.Class _ (Term (In identifierAnn _), _) _ _) - = Just $ ClassDeclaration (getSource blobSource identifierAnn) classSource (locationSpan ann) blobLanguage - where classSource = getIdentifier (arr Declaration.classBody) blob (In ann decl) - -instance CustomHasDeclaration whole Ruby.Syntax.Class where - customToDeclaration blob@Blob{..} ann decl@(Ruby.Syntax.Class (Term (In identifierAnn _), _) _ _) - = Just $ ClassDeclaration (getSource blobSource identifierAnn) rubyClassSource (locationSpan ann) blobLanguage - where rubyClassSource = getIdentifier (arr Ruby.Syntax.classBody) blob (In ann decl) - -instance CustomHasDeclaration whole Ruby.Syntax.Module where - customToDeclaration blob@Blob{..} ann decl@(Ruby.Syntax.Module (Term (In identifierAnn _), _) _) - = Just $ ModuleDeclaration (getSource blobSource identifierAnn) rubyModuleSource (locationSpan ann) blobLanguage - where rubyModuleSource = getIdentifier (arr Ruby.Syntax.moduleStatements >>> first) blob (In ann decl) - -instance CustomHasDeclaration whole TypeScript.Syntax.Module where - customToDeclaration blob@Blob{..} ann decl@(TypeScript.Syntax.Module (Term (In identifierAnn _), _) _) - = Just $ ModuleDeclaration (getSource blobSource identifierAnn) tsModuleSource (locationSpan ann) blobLanguage - where tsModuleSource = getIdentifier (arr TypeScript.Syntax.moduleStatements >>> first) blob (In ann decl) - -- When encountering a Declaration-annotated term, we need to extract a Text -- for the resulting Declaration's 'declarationIdentifier' field. This text -- is constructed by slicing out text from the original blob corresponding @@ -154,9 +128,6 @@ getIdentifier finder Blob{..} (In a r) sliceFrom = T.stripEnd . toText . flip Source.slice blobSource . subtractRange declRange in either (const mempty) sliceFrom bodyRange -first :: Rule env [a] a -first = target >>= maybeM (Prologue.fail "empty list") . listToMaybe - getSource :: Source -> Location -> Text getSource blobSource = toText . flip Source.slice blobSource . locationByteRange @@ -181,10 +152,6 @@ class HasDeclarationWithStrategy (strategy :: Strategy) whole syntax where -- -- If you’re seeing errors about missing a 'CustomHasDeclaration' instance for a given type, you’ve probably listed it in here but not defined a 'CustomHasDeclaration' instance for it, or else you’ve listed the wrong type in here. Conversely, if your 'customHasDeclaration' method is never being called, you may have forgotten to list the type in here. type family DeclarationStrategy syntax where - DeclarationStrategy Declaration.Class = 'Custom - DeclarationStrategy Ruby.Syntax.Class = 'Custom - DeclarationStrategy Ruby.Syntax.Module = 'Custom - DeclarationStrategy TypeScript.Syntax.Module = 'Custom DeclarationStrategy Declaration.Function = 'Custom DeclarationStrategy Declaration.Method = 'Custom DeclarationStrategy Markdown.Heading = 'Custom diff --git a/src/Rendering/TOC.hs b/src/Rendering/TOC.hs index 8c1314467..ff991959e 100644 --- a/src/Rendering/TOC.hs +++ b/src/Rendering/TOC.hs @@ -17,7 +17,7 @@ module Rendering.TOC ) where import Prologue -import Analysis.Declaration +import Analysis.TOCSummary import Data.Align (bicrosswalk) import Data.Aeson import Data.Blob @@ -151,13 +151,7 @@ renderRPCToCDiff :: (Foldable f, Functor f) => BlobPair -> Diff f (Maybe Declara renderRPCToCDiff _ = List.partition isValidSummary . diffTOC diffTOC :: (Foldable f, Functor f) => Diff f (Maybe Declaration) (Maybe Declaration) -> [TOCSummary] -diffTOC = fmap entrySummary . dedupe . filter extraDeclarations . tableOfContentsBy declaration - where - extraDeclarations :: Entry Declaration -> Bool - extraDeclarations entry = case entryPayload entry of - ClassDeclaration{..} -> False - ModuleDeclaration{..} -> False - _ -> True +diffTOC = fmap entrySummary . dedupe . tableOfContentsBy declaration renderToCTerm :: (Foldable f, Functor f) => Blob -> Term f (Maybe Declaration) -> Summaries renderToCTerm Blob{..} = uncurry Summaries . bimap toMap toMap . List.partition isValidSummary . termToC @@ -171,8 +165,6 @@ renderToCTerm Blob{..} = uncurry Summaries . bimap toMap toMap . List.partition -- The user-facing category name toCategoryName :: Declaration -> T.Text toCategoryName declaration = case declaration of - ClassDeclaration{} -> "Class" - ModuleDeclaration{} -> "Module" FunctionDeclaration{} -> "Function" MethodDeclaration{} -> "Method" HeadingDeclaration _ _ _ _ l -> "Heading " <> T.pack (show l) diff --git a/src/Semantic/Diff.hs b/src/Semantic/Diff.hs index 99bb75528..b1be712e3 100644 --- a/src/Semantic/Diff.hs +++ b/src/Semantic/Diff.hs @@ -5,7 +5,7 @@ module Semantic.Diff ) where import Analysis.ConstructorName (ConstructorName) -import Analysis.Declaration (HasDeclaration, declarationAlgebra) +import Analysis.TOCSummary (HasDeclaration, declarationAlgebra) import Control.Effect import Control.Monad.IO.Class import Data.Blob diff --git a/test/Data/Functor/Listable.hs b/test/Data/Functor/Listable.hs index 1076eb393..db70f838d 100644 --- a/test/Data/Functor/Listable.hs +++ b/test/Data/Functor/Listable.hs @@ -18,7 +18,7 @@ module Data.Functor.Listable ) where import Analysis.CyclomaticComplexity -import Analysis.Declaration +import Analysis.TOCSummary import Control.Monad.Free as Free import Control.Monad.Trans.Free as FreeF import Data.ByteString (ByteString) diff --git a/test/Rendering/TOC/Spec.hs b/test/Rendering/TOC/Spec.hs index d6669f667..bbc562431 100644 --- a/test/Rendering/TOC/Spec.hs +++ b/test/Rendering/TOC/Spec.hs @@ -1,7 +1,7 @@ {-# LANGUAGE DataKinds, MonoLocalBinds, TypeOperators #-} module Rendering.TOC.Spec (spec) where -import Analysis.Declaration +import Analysis.TOCSummary import Control.Effect import Data.Aeson hiding (defaultOptions) import Data.Bifunctor