Skip to content

Commit

Permalink
Merge pull request yi-editor#8 from asmyers/git-commit-mode
Browse files Browse the repository at this point in the history
Mode for git commit messages.
  • Loading branch information
reinerp committed Jul 8, 2011
2 parents 3ec3429 + 34e3c83 commit 31bfa08
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
1 change: 1 addition & 0 deletions yi/src/library/Yi/Config/Default.hs
Expand Up @@ -166,6 +166,7 @@ defaultConfig =
AnyMode javaMode,
AnyMode ireaderMode,
AnyMode svnCommitMode,
AnyMode gitCommitMode,
AnyMode whitespaceMode,
AnyMode fundamentalMode]
, debugMode = False
Expand Down
100 changes: 100 additions & 0 deletions yi/src/library/Yi/Lexer/GitCommit.x
@@ -0,0 +1,100 @@
-- -*- haskell -*-
-- Maintainer: Andrew Myers
{
{-# OPTIONS -w #-}
module Yi.Lexer.GitCommit
( initState, alexScanToken )
where
import Data.Monoid (mappend)
import Yi.Lexer.Alex
import Yi.Style ( StyleName )
import qualified Yi.Style as Style
}

$commitChars = [$printable\t] # [\#]
@diffStart = diff\ \-\-git\ $commitChars*
$nl = [\n\r]
$notColon = $printable # [:]

gitCommit :-

-- The first line of a git commit message is the digest that is
-- displayed as a summary of the commit in virtually all git tools.
<0> {
^.*$ { c Style.regexStyle }
$nl { m (const SecondLine) Style.defaultStyle }
}

-- There should never be anything on the second line of a git commit message
-- so it is styled in a deliberately hideous color scheme.
<secondLine> {
.* { c (const $ Style.withFg Style.red `mappend` Style.withBg Style.brown) }
$nl { m (const MessageLine) Style.defaultStyle }
}

-- The body of a commit message is broken up as follows
-- * User's message
-- * git generated information in comments
-- * optional diff if commit was run with the -v option.
<body> {
^@diffStart$ { m (const $ DiffDeclaration) Style.regexStyle }
\# { m (const $ LineComment) Style.commentStyle }
$commitChars*$ { c Style.defaultStyle }
$white { c Style.defaultStyle }
. { c Style.defaultStyle }
}

-- Inside git generated comments specific information about what this
-- commit will do is displayed. Highlight keywords and filenames.
-- The notColon rule highlights filenames not preceded by keywords.
-- The specific keywords rules switch to <keyword> context to highlight
-- everything to the end of the line (which should only ever be a filename.)
<lineComment> {
$nl { m (const MessageLine) Style.defaultStyle }
\t$notColon+$ { c Style.preprocessorStyle }
"modified:" { m (const Keyword) Style.keywordStyle }
"new file:" { m (const Keyword) Style.keywordStyle }
"deleted:" { m (const Keyword) Style.keywordStyle }
. { c Style.commentStyle }
}

<keyword> {
$nl { m (const MessageLine) Style.defaultStyle }
. { c Style.preprocessorStyle }
}

-- Highlight diff lines
<diff> {
^@diffStart$ { c Style.regexStyle }
^\@\@.* { c Style.keywordStyle }
^\- .*$ { c Style.commentStyle }
^\+ .*$ { c Style.operatorStyle }
^.*$ { c Style.defaultStyle }
$white { c Style.defaultStyle }
. { c Style.defaultStyle }
}

{

data HlState = Digest
| SecondLine
| Keyword
| MessageLine
| LineComment
| DiffDeclaration
deriving (Show, Eq)

stateToInit Digest = 0
stateToInit SecondLine = secondLine
stateToInit Keyword = keyword
stateToInit MessageLine = body
stateToInit DiffDeclaration = diff
stateToInit LineComment = lineComment

initState :: HlState
initState = Digest

type Token = StyleName
#include "common.hsinc"
}

13 changes: 11 additions & 2 deletions yi/src/library/Yi/Modes.hs
Expand Up @@ -5,11 +5,12 @@ module Yi.Modes (TokenBasedMode, fundamentalMode,
perlMode, pythonMode, javaMode, anyExtension,
extensionOrContentsMatch, linearSyntaxMode,
svnCommitMode, hookModes, applyModeHooks,
lookupMode, whitespaceMode, removeAnnots
lookupMode, whitespaceMode, removeAnnots,
gitCommitMode
) where

import Prelude ()
import Data.List ( isPrefixOf, map, filter )
import Data.List ( isPrefixOf, isSuffixOf, map, filter )
import Data.Maybe
import System.FilePath
import Text.Regex.TDFA ((=~))
Expand All @@ -36,6 +37,7 @@ import qualified Yi.Lexer.Python as Python
import qualified Yi.Lexer.Java as Java
import qualified Yi.Lexer.Srmc as Srmc
import qualified Yi.Lexer.SVNCommit as SVNCommit
import qualified Yi.Lexer.GitCommit as GitCommit
import qualified Yi.Lexer.Whitespace as Whitespace
import Yi.Syntax.OnlineTree as OnlineTree
import qualified Yi.IncrementalParse as IncrParser
Expand Down Expand Up @@ -110,6 +112,13 @@ srmcMode = (linearSyntaxMode Srmc.initState Srmc.alexScanToken id)
"srmc"]
}

gitCommitMode = (linearSyntaxMode GitCommit.initState GitCommit.alexScanToken id)
{
modeName = "git-commit",
modeApplies = \path _ -> takeFileName path == "COMMIT_EDITMSG" &&
".git" `isSuffixOf`(takeDirectory path)
}

svnCommitMode = (linearSyntaxMode SVNCommit.initState SVNCommit.alexScanToken id)
{
modeName = "svn-commit",
Expand Down
1 change: 1 addition & 0 deletions yi/yi.cabal
Expand Up @@ -131,6 +131,7 @@ library
Yi.Lexer.Java
Yi.Lexer.Latex
Yi.Lexer.LiterateHaskell
Yi.Lexer.GitCommit
Yi.Lexer.GNUMake
Yi.Lexer.OCaml
Yi.Lexer.Ott
Expand Down

0 comments on commit 31bfa08

Please sign in to comment.