mirror of
https://github.com/github/semantic.git
synced 2024-11-28 01:47:01 +03:00
Rename locByteRange/locSpan to byteRange/span.
This commit is contained in:
parent
955174211c
commit
52bc7e640e
@ -9,12 +9,13 @@ import Control.DeepSeq (NFData)
|
||||
import Data.Hashable (Hashable)
|
||||
import Data.Monoid.Generic
|
||||
import GHC.Generics (Generic)
|
||||
import Prelude hiding (span)
|
||||
import Source.Range
|
||||
import Source.Span
|
||||
|
||||
data Loc = Loc
|
||||
{ locByteRange :: {-# UNPACK #-} !Range
|
||||
, locSpan :: {-# UNPACK #-} !Span
|
||||
{ byteRange :: {-# UNPACK #-} !Range
|
||||
, span :: {-# UNPACK #-} !Span
|
||||
}
|
||||
deriving (Eq, Ord, Show, Generic)
|
||||
deriving Semigroup via GenericSemigroup Loc
|
||||
@ -23,6 +24,6 @@ instance Hashable Loc
|
||||
instance NFData Loc
|
||||
|
||||
instance HasSpan Loc where
|
||||
span_ = lens locSpan (\l s -> l { locSpan = s }) where
|
||||
span_ = lens span (\l s -> l { span = s }) where
|
||||
lens get put afa s = fmap (put s) (afa (get s))
|
||||
{-# INLINE span_ #-}
|
||||
|
@ -56,7 +56,7 @@ class CustomHasPackageDef syntax where
|
||||
instance CustomHasPackageDef Language.Go.Syntax.Package where
|
||||
customToPackageDef Blob{..} _ (Language.Go.Syntax.Package (Term (In fromAnn _), _) _)
|
||||
= Just $ PackageDef (getSource fromAnn)
|
||||
where getSource = toText . Source.slice blobSource . locByteRange
|
||||
where getSource = toText . Source.slice blobSource . byteRange
|
||||
|
||||
-- | Produce a 'PackageDef' for 'Sum's using the 'HasPackageDef' instance & therefore using a 'CustomHasPackageDef' instance when one exists & the type is listed in 'PackageDefStrategy'.
|
||||
instance Apply HasPackageDef fs => CustomHasPackageDef (Sum fs) where
|
||||
|
@ -18,7 +18,7 @@ import qualified Data.Syntax as Syntax
|
||||
import qualified Data.Syntax.Declaration as Declaration
|
||||
import Data.Term
|
||||
import qualified Data.Text as T
|
||||
import Source.Loc
|
||||
import Source.Loc as Loc
|
||||
import Source.Range
|
||||
import qualified Language.Markdown.Syntax as Markdown
|
||||
|
||||
@ -77,16 +77,16 @@ class CustomHasDeclaration whole syntax where
|
||||
-- | Produce a 'HeadingDeclaration' from the first line of the heading of a 'Markdown.Heading' node.
|
||||
instance CustomHasDeclaration whole Markdown.Heading where
|
||||
customToDeclaration blob@Blob{..} ann (Markdown.Heading level terms _)
|
||||
= Just $ HeadingDeclaration (headingText terms) mempty (locSpan ann) (blobLanguage blob) level
|
||||
where headingText terms = getSource $ maybe (locByteRange ann) sconcat (nonEmpty (headingByteRange <$> toList terms))
|
||||
headingByteRange (Term (In ann _), _) = locByteRange ann
|
||||
= Just $ HeadingDeclaration (headingText terms) mempty (Loc.span ann) (blobLanguage blob) level
|
||||
where headingText terms = getSource $ maybe (byteRange ann) sconcat (nonEmpty (headingByteRange <$> toList terms))
|
||||
headingByteRange (Term (In ann _), _) = byteRange ann
|
||||
getSource = firstLine . toText . Source.slice blobSource
|
||||
firstLine = T.takeWhile (/= '\n')
|
||||
|
||||
-- | Produce an 'ErrorDeclaration' for 'Syntax.Error' nodes.
|
||||
instance CustomHasDeclaration whole Syntax.Error where
|
||||
customToDeclaration blob@Blob{..} ann err@Syntax.Error{}
|
||||
= Just $ ErrorDeclaration (T.pack (formatTOCError (Syntax.unError (locSpan ann) err))) mempty (locSpan ann) (blobLanguage blob)
|
||||
= Just $ ErrorDeclaration (T.pack (formatTOCError (Syntax.unError (Loc.span ann) err))) mempty (Loc.span ann) (blobLanguage blob)
|
||||
where formatTOCError e = showExpectation (flag Colourize False) (errorExpected e) (errorActual e) ""
|
||||
|
||||
-- | Produce a 'FunctionDeclaration' for 'Declaration.Function' nodes so long as their identifier is non-empty (defined as having a non-empty 'Range').
|
||||
@ -95,22 +95,22 @@ instance CustomHasDeclaration whole Declaration.Function where
|
||||
-- Do not summarize anonymous functions
|
||||
| isEmpty identifierAnn = Nothing
|
||||
-- Named functions
|
||||
| otherwise = Just $ FunctionDeclaration (getSource blobSource identifierAnn) functionSource (locSpan ann) (blobLanguage blob)
|
||||
where isEmpty = (== 0) . rangeLength . locByteRange
|
||||
| otherwise = Just $ FunctionDeclaration (getSource blobSource identifierAnn) functionSource (Loc.span ann) (blobLanguage blob)
|
||||
where isEmpty = (== 0) . rangeLength . byteRange
|
||||
functionSource = getIdentifier (arr Declaration.functionBody) blob (In ann decl)
|
||||
|
||||
-- | Produce a 'MethodDeclaration' for 'Declaration.Method' nodes. If the method’s receiver is non-empty (defined as having a non-empty 'Range'), the 'declarationIdentifier' will be formatted as 'receiver.method_name'; otherwise it will be simply 'method_name'.
|
||||
instance CustomHasDeclaration whole Declaration.Method where
|
||||
customToDeclaration blob@Blob{..} ann decl@(Declaration.Method _ (Term (In receiverAnn receiverF), _) (Term (In identifierAnn _), _) _ _ _)
|
||||
-- Methods without a receiver
|
||||
| isEmpty receiverAnn = Just $ MethodDeclaration (getSource blobSource identifierAnn) methodSource (locSpan ann) (blobLanguage blob) Nothing
|
||||
| isEmpty receiverAnn = Just $ MethodDeclaration (getSource blobSource identifierAnn) methodSource (Loc.span ann) (blobLanguage blob) Nothing
|
||||
-- Methods with a receiver type and an identifier (e.g. (a *Type) in Go).
|
||||
| blobLanguage blob == Go
|
||||
, [ _, Term (In receiverType _) ] <- toList receiverF = Just $ MethodDeclaration (getSource blobSource identifierAnn) methodSource (locSpan ann) (blobLanguage blob) (Just (getSource blobSource receiverType))
|
||||
, [ _, Term (In receiverType _) ] <- toList receiverF = Just $ MethodDeclaration (getSource blobSource identifierAnn) methodSource (Loc.span ann) (blobLanguage blob) (Just (getSource blobSource receiverType))
|
||||
-- Methods with a receiver (class methods) are formatted like `receiver.method_name`
|
||||
| otherwise = Just $ MethodDeclaration (getSource blobSource identifierAnn) methodSource (locSpan ann) (blobLanguage blob) (Just (getSource blobSource receiverAnn))
|
||||
| otherwise = Just $ MethodDeclaration (getSource blobSource identifierAnn) methodSource (Loc.span ann) (blobLanguage blob) (Just (getSource blobSource receiverAnn))
|
||||
where
|
||||
isEmpty = (== 0) . rangeLength . locByteRange
|
||||
isEmpty = (== 0) . rangeLength . byteRange
|
||||
methodSource = getIdentifier (arr Declaration.methodBody) blob (In ann decl)
|
||||
|
||||
-- When encountering a Declaration-annotated term, we need to extract a Text
|
||||
@ -123,14 +123,14 @@ getIdentifier :: Functor m
|
||||
-> TermF m Loc (Term syntax Loc, a)
|
||||
-> Text
|
||||
getIdentifier finder Blob{..} (In a r)
|
||||
= let declRange = locByteRange a
|
||||
bodyRange = locByteRange <$> rewrite (fmap fst r) (finder >>^ annotation)
|
||||
= let declRange = byteRange a
|
||||
bodyRange = byteRange <$> rewrite (fmap fst r) (finder >>^ annotation)
|
||||
-- Text-based gyrations to slice the identifier out of the provided blob source
|
||||
sliceFrom = T.stripEnd . toText . Source.slice blobSource . subtractRange declRange
|
||||
in maybe mempty sliceFrom bodyRange
|
||||
|
||||
getSource :: Source -> Loc -> Text
|
||||
getSource blobSource = toText . Source.slice blobSource . locByteRange
|
||||
getSource blobSource = toText . Source.slice blobSource . byteRange
|
||||
|
||||
-- | Produce a 'Declaration' for 'Sum's using the 'HasDeclaration' instance & therefore using a 'CustomHasDeclaration' instance when one exists & the type is listed in 'DeclarationStrategy'.
|
||||
instance Apply (HasDeclaration' whole) fs => CustomHasDeclaration whole (Sum fs) where
|
||||
|
@ -10,7 +10,7 @@ import Data.Term
|
||||
import Data.Aeson
|
||||
import Data.Text (pack)
|
||||
import Data.JSON.Fields
|
||||
import Source.Loc
|
||||
import Source.Loc as Loc
|
||||
|
||||
-- | An AST node labelled with symbols and source location.
|
||||
type AST syntax grammar = Term syntax (Node grammar)
|
||||
@ -25,11 +25,11 @@ data Node grammar = Node
|
||||
instance Show grammar => ToJSONFields (Node grammar) where
|
||||
toJSONFields Node{..} =
|
||||
[ "symbol" .= pack (show nodeSymbol)
|
||||
, "span" .= locSpan nodeLocation
|
||||
, "span" .= Loc.span nodeLocation
|
||||
]
|
||||
|
||||
nodeSpan :: Node grammar -> Span
|
||||
nodeSpan = locSpan . nodeLocation
|
||||
nodeSpan = Loc.span . nodeLocation
|
||||
|
||||
nodeByteRange :: Node grammar -> Range
|
||||
nodeByteRange = locByteRange . nodeLocation
|
||||
nodeByteRange = byteRange . nodeLocation
|
||||
|
@ -28,7 +28,7 @@ import qualified Data.Syntax.Expression as Expression
|
||||
import Data.Term
|
||||
import qualified Data.Text as T
|
||||
import Prologue
|
||||
import Source.Loc
|
||||
import Source.Loc as Loc
|
||||
import Source.Span
|
||||
|
||||
-- | A vertex of representing some node in a control flow graph.
|
||||
@ -145,17 +145,17 @@ instance Apply (VertexDeclaration' whole) fs => VertexDeclarationWithStrategy 'C
|
||||
toVertexWithStrategy _ ann info = apply @(VertexDeclaration' whole) (toVertex' ann info)
|
||||
|
||||
instance VertexDeclarationWithStrategy 'Custom whole Syntax.Identifier where
|
||||
toVertexWithStrategy _ ann info (Syntax.Identifier name) = Just (variableVertex (formatName name) info (locSpan ann), name)
|
||||
toVertexWithStrategy _ ann info (Syntax.Identifier name) = Just (variableVertex (formatName name) info (Loc.span ann), name)
|
||||
|
||||
instance VertexDeclarationWithStrategy 'Custom whole Declaration.Function where
|
||||
toVertexWithStrategy _ ann info term@Declaration.Function{} = (\n -> (functionVertex (formatName n) info (locSpan ann), n)) <$> liftDeclaredName declaredName term
|
||||
toVertexWithStrategy _ ann info term@Declaration.Function{} = (\n -> (functionVertex (formatName n) info (Loc.span ann), n)) <$> liftDeclaredName declaredName term
|
||||
|
||||
instance VertexDeclarationWithStrategy 'Custom whole Declaration.Method where
|
||||
toVertexWithStrategy _ ann info term@Declaration.Method{} = (\n -> (methodVertex (formatName n) info (locSpan ann), n)) <$> liftDeclaredName declaredName term
|
||||
toVertexWithStrategy _ ann info term@Declaration.Method{} = (\n -> (methodVertex (formatName n) info (Loc.span ann), n)) <$> liftDeclaredName declaredName term
|
||||
|
||||
instance VertexDeclarationWithStrategy 'Custom whole whole => VertexDeclarationWithStrategy 'Custom whole Expression.MemberAccess where
|
||||
toVertexWithStrategy proxy ann info (Expression.MemberAccess (Term (In lhsAnn lhs)) (Term (In rhsAnn rhs))) =
|
||||
case (toVertexWithStrategy proxy lhsAnn info lhs, toVertexWithStrategy proxy rhsAnn info rhs) of
|
||||
(Just (Variable n _ _, _), Just (_, name)) -> Just (variableVertex (n <> "." <> formatName name) info (locSpan ann), name)
|
||||
(_, Just (_, name)) -> Just (variableVertex (formatName name) info (locSpan ann), name)
|
||||
(Just (Variable n _ _, _), Just (_, name)) -> Just (variableVertex (n <> "." <> formatName name) info (Loc.span ann), name)
|
||||
(_, Just (_, name)) -> Just (variableVertex (formatName name) info (Loc.span ann), name)
|
||||
_ -> Nothing
|
||||
|
@ -23,7 +23,7 @@ mark :: Functor f
|
||||
=> (Range -> History)
|
||||
-> f Loc
|
||||
-> f History
|
||||
mark f = fmap (f . locByteRange)
|
||||
mark f = fmap (f . byteRange)
|
||||
|
||||
-- | Change the 'History' annotation on a 'Term'.
|
||||
remark :: Functor f
|
||||
|
@ -55,7 +55,7 @@ instance ToJSONFields Span where
|
||||
toJSONFields sourceSpan = [ "sourceSpan" .= sourceSpan ]
|
||||
|
||||
instance ToJSONFields Loc where
|
||||
toJSONFields Loc{..} = toJSONFields locByteRange <> toJSONFields locSpan
|
||||
toJSONFields Loc{..} = toJSONFields byteRange <> toJSONFields span
|
||||
|
||||
|
||||
newtype JSONFields a = JSONFields { unJSONFields :: a }
|
||||
|
@ -53,7 +53,7 @@ makeTerm1' syntax = case toList syntax of
|
||||
-- | Construct an empty term at the current position.
|
||||
emptyTerm :: (HasCallStack, Empty :< syntaxes, Apply Foldable syntaxes) => Assignment.Assignment ast grammar (Term (Sum syntaxes) Loc)
|
||||
emptyTerm = makeTerm . startLocation <$> Assignment.location <*> pure Empty
|
||||
where startLocation Loc{..} = Loc (Range.point (Range.start locByteRange)) (Span.point (Span.start locSpan))
|
||||
where startLocation Loc{..} = Loc (Range.point (Range.start byteRange)) (Span.point (Span.start span))
|
||||
|
||||
-- | Catch assignment errors into an error term.
|
||||
handleError :: (HasCallStack, Error :< syntaxes, Enum grammar, Eq1 ast, Ix grammar, Show grammar, Apply Foldable syntaxes) => Assignment.Assignment ast grammar (Term (Sum syntaxes) Loc) -> Assignment.Assignment ast grammar (Term (Sum syntaxes) Loc)
|
||||
|
@ -20,7 +20,7 @@ import Data.Term
|
||||
import Prologue
|
||||
import Semantic.Api.Bridge
|
||||
import Semantic.Proto.SemanticPB
|
||||
import Source.Loc
|
||||
import Source.Loc as Loc
|
||||
|
||||
import qualified Data.Text as T
|
||||
|
||||
@ -75,7 +75,7 @@ instance (ConstructorName syntax, Foldable syntax) =>
|
||||
termAlgebra (In ann syntax) = do
|
||||
i <- fresh
|
||||
parent <- ask
|
||||
let root = vertex $ TermVertex (fromIntegral i) (T.pack (constructorName syntax)) (converting #? locSpan ann)
|
||||
let root = vertex $ TermVertex (fromIntegral i) (T.pack (constructorName syntax)) (converting #? Loc.span ann)
|
||||
subGraph <- foldl' (\acc x -> overlay <$> acc <*> local (const root) x) (pure mempty) syntax
|
||||
pure (parent `connect` root `overlay` subGraph)
|
||||
|
||||
@ -94,7 +94,7 @@ instance (ConstructorName syntax, Foldable syntax) =>
|
||||
graph <- local (const replace) (overlay <$> diffAlgebra t1 (Deleted (Just (DeletedTerm beforeName beforeSpan))) <*> diffAlgebra t2 (Inserted (Just (InsertedTerm afterName afterSpan))))
|
||||
pure (parent `connect` replace `overlay` graph)
|
||||
where
|
||||
ann a = converting #? locSpan a
|
||||
ann a = converting #? Loc.span a
|
||||
diffAlgebra ::
|
||||
( Foldable f
|
||||
, Member Fresh sig
|
||||
|
@ -63,7 +63,7 @@ import Parsing.Parser
|
||||
import Prologue hiding (TypeError (..))
|
||||
import Semantic.Analysis
|
||||
import Semantic.Task as Task
|
||||
import Source.Loc
|
||||
import Source.Loc as Loc
|
||||
import Source.Span
|
||||
import System.FilePath.Posix (takeDirectory, (</>))
|
||||
import Text.Show.Pretty (ppShow)
|
||||
@ -339,7 +339,7 @@ withTermSpans :: ( Member (Reader Span) sig
|
||||
)
|
||||
=> Open (term -> Evaluator term address value m a)
|
||||
withTermSpans recur term = let
|
||||
span = locSpan (termFAnnotation (project term))
|
||||
span = Loc.span (termFAnnotation (project term))
|
||||
updatedSpanAlg = withCurrentSpan span (recur term)
|
||||
in modifyChildSpan span updatedSpanAlg
|
||||
|
||||
|
@ -280,7 +280,7 @@ runParser blob@Blob{..} parser = case parser of
|
||||
where languageTag = pure . (,) ("language" :: String) . show $ blobLanguage blob
|
||||
errors :: (Syntax.Error :< fs, Apply Foldable fs, Apply Functor fs) => Term (Sum fs) Assignment.Loc -> [Error.Error String]
|
||||
errors = cata $ \ (In Assignment.Loc{..} syntax) -> case syntax of
|
||||
_ | Just err@Syntax.Error{} <- project syntax -> [Syntax.unError locSpan err]
|
||||
_ | Just err@Syntax.Error{} <- project syntax -> [Syntax.unError span err]
|
||||
_ -> fold syntax
|
||||
runAssignment :: ( Apply Foldable syntaxes
|
||||
, Apply Functor syntaxes
|
||||
|
@ -33,7 +33,7 @@ import Data.Blob
|
||||
import Data.Language
|
||||
import Data.Term
|
||||
import Data.Text hiding (empty)
|
||||
import Source.Loc
|
||||
import Source.Loc as Loc
|
||||
import Source.Range
|
||||
|
||||
import Streaming hiding (Sum)
|
||||
@ -116,12 +116,12 @@ descend lang t@(In loc _) = do
|
||||
let litRange = docsLiteral lang term
|
||||
|
||||
enter (constructorName term) snippetRange
|
||||
maybe (pure ()) (emitIden (locSpan loc) litRange) (symbolName term)
|
||||
maybe (pure ()) (emitIden (Loc.span loc) litRange) (symbolName term)
|
||||
traverse_ subtermRef t
|
||||
exit (constructorName term) snippetRange
|
||||
|
||||
subtractLoc :: Loc -> Loc -> Range
|
||||
subtractLoc a b = subtractRange (locByteRange a) (locByteRange b)
|
||||
subtractLoc a b = subtractRange (byteRange a) (byteRange b)
|
||||
|
||||
-- Instances
|
||||
|
||||
@ -162,7 +162,7 @@ instance TaggableBy 'Custom Syntax.Context where
|
||||
instance TaggableBy 'Custom Declaration.Function where
|
||||
docsLiteral' Python (Declaration.Function _ _ _ (Term (In _ bodyF)))
|
||||
| (Term (In exprAnn exprF):_) <- toList bodyF
|
||||
, isTextElement exprF = Just (locByteRange exprAnn)
|
||||
, isTextElement exprF = Just (byteRange exprAnn)
|
||||
| otherwise = Nothing
|
||||
docsLiteral' _ _ = Nothing
|
||||
snippet' ann (Declaration.Function _ _ _ (Term (In body _))) = Just $ subtractLoc ann body
|
||||
@ -171,7 +171,7 @@ instance TaggableBy 'Custom Declaration.Function where
|
||||
instance TaggableBy 'Custom Declaration.Method where
|
||||
docsLiteral' Python (Declaration.Method _ _ _ _ (Term (In _ bodyF)) _)
|
||||
| (Term (In exprAnn exprF):_) <- toList bodyF
|
||||
, isTextElement exprF = Just (locByteRange exprAnn)
|
||||
, isTextElement exprF = Just (byteRange exprAnn)
|
||||
| otherwise = Nothing
|
||||
docsLiteral' _ _ = Nothing
|
||||
snippet' ann (Declaration.Method _ _ _ _ (Term (In body _)) _) = Just $ subtractLoc ann body
|
||||
@ -180,7 +180,7 @@ instance TaggableBy 'Custom Declaration.Method where
|
||||
instance TaggableBy 'Custom Declaration.Class where
|
||||
docsLiteral' Python (Declaration.Class _ _ _ (Term (In _ bodyF)))
|
||||
| (Term (In exprAnn exprF):_) <- toList bodyF
|
||||
, isTextElement exprF = Just (locByteRange exprAnn)
|
||||
, isTextElement exprF = Just (byteRange exprAnn)
|
||||
| otherwise = Nothing
|
||||
docsLiteral' _ _ = Nothing
|
||||
snippet' ann (Declaration.Class _ _ _ (Term (In body _))) = Just $ subtractLoc ann body
|
||||
@ -192,12 +192,12 @@ instance TaggableBy 'Custom Ruby.Class where
|
||||
|
||||
instance TaggableBy 'Custom Ruby.Module where
|
||||
snippet' ann (Ruby.Module _ (Term (In body _):_)) = Just $ subtractLoc ann body
|
||||
snippet' ann (Ruby.Module _ _) = Just $ locByteRange ann
|
||||
snippet' ann (Ruby.Module _ _) = Just $ byteRange ann
|
||||
symbolName' = declaredName . Ruby.moduleIdentifier
|
||||
|
||||
instance TaggableBy 'Custom TypeScript.Module where
|
||||
snippet' ann (TypeScript.Module _ (Term (In body _):_)) = Just $ subtractLoc ann body
|
||||
snippet' ann (TypeScript.Module _ _ ) = Just $ locByteRange ann
|
||||
snippet' ann (TypeScript.Module _ _ ) = Just $ byteRange ann
|
||||
symbolName' = declaredName . TypeScript.moduleIdentifier
|
||||
|
||||
instance TaggableBy 'Custom Expression.Call where
|
||||
@ -206,5 +206,5 @@ instance TaggableBy 'Custom Expression.Call where
|
||||
|
||||
instance TaggableBy 'Custom Ruby.Send where
|
||||
snippet' ann (Ruby.Send _ _ _ (Just (Term (In body _)))) = Just $ subtractLoc ann body
|
||||
snippet' ann _ = Just $ locByteRange ann
|
||||
snippet' ann _ = Just $ byteRange ann
|
||||
symbolName' Ruby.Send{..} = declaredName =<< sendSelector
|
||||
|
@ -53,8 +53,8 @@ import Parsing.Parser as X
|
||||
import Semantic.Task as X
|
||||
import Semantic.Util as X
|
||||
import Semantic.Graph (runHeap, runScopeGraph)
|
||||
import Source.Range as X
|
||||
import Source.Span as X hiding (HasSpan(..))
|
||||
import Source.Range as X hiding (start, end, point)
|
||||
import Source.Span as X hiding (HasSpan(..), start, end, point)
|
||||
import Debug.Trace as X (traceShowM, traceM)
|
||||
|
||||
import Data.ByteString as X (ByteString)
|
||||
|
Loading…
Reference in New Issue
Block a user