headroom-0.4.0.0: License Header Manager
Copyright(c) 2019-2021 Vaclav Svejcar
LicenseBSD-3-Clause
Maintainervaclav.svejcar@gmail.com
Stabilityexperimental
PortabilityPOSIX
Safe HaskellNone
LanguageHaskell2010

Headroom.SourceCode

Description

This module contains data types and function used for analysis and type safe representation of source code files.

Synopsis

Data Types

data LineType Source #

Represents type of the line in source code.

Constructors

Code

Line of code

Comment

Line of comment

Instances

Instances details
Eq LineType Source # 
Instance details

Defined in Headroom.SourceCode

Show LineType Source # 
Instance details

Defined in Headroom.SourceCode

type CodeLine = (LineType, Text) Source #

Type alias for analyzed line of code.

newtype SourceCode Source #

Represents analyzed source code.

Constructors

SourceCode [CodeLine] 

Functions

fromText Source #

Arguments

:: a

initial state of analyzing function

-> (Text -> State a LineType)

function that analyzes currently processed line

-> Text

raw source code to analyze

-> SourceCode

analyzed SourceCode

Converts Text into SourceCode using the given function to analyze each line's LineType. The analyzing function can hold any state that is accumulated as the text is processed, for example to hold some info about already processed lines.

toText Source #

Arguments

:: SourceCode

source code to convert back to plain text

-> Text

resulting plain text

Converts analyzed SourceCode back into Text.

firstMatching Source #

Arguments

:: (CodeLine -> Maybe a)

predicate (and transform) function

-> SourceCode

source code to search in

-> Maybe (Int, a)

first matching line (if found)

Finds very first line matching given predicate and optionally performs some operation over it.

lastMatching Source #

Arguments

:: (CodeLine -> Maybe a)

predicate (and transform) function

-> SourceCode

source code to search in

-> Maybe (Int, a)

last matching line (if found)

Finds very last line matching given predicate and optionally performs some operation over it.

stripStart Source #

Arguments

:: SourceCode

source code to strip

-> SourceCode

stripped source code

Strips empty lines at the beginning of source code.

>>> stripStart $ SourceCode [(Code, ""), (Code, "foo"), (Code, "")]
SourceCode [(Code,"foo"),(Code,"")]

stripEnd Source #

Arguments

:: SourceCode

source code to strip

-> SourceCode

stripped source code

Strips empty lines at the end of source code.

>>> stripEnd $ SourceCode [(Code, ""), (Code, "foo"), (Code, "")]
SourceCode [(Code,""),(Code,"foo")]

cut Source #

Arguments

:: Int

index of first line to be included into the snippet

-> Int

index of the first line after the snippet

-> SourceCode

source code to cut

-> SourceCode

cut snippet

Cuts snippet from the source code using the given start and end position.

>>> cut 1 3 $ SourceCode [(Code, "1"), (Code, "2"),(Code, "3"),(Code, "4")]
SourceCode [(Code,"2"),(Code,"3")]