Skip to content

Commit

Permalink
Add live template contexts and some default live templates
Browse files Browse the repository at this point in the history
  • Loading branch information
ice1000 committed Nov 24, 2018
1 parent 3e7399f commit 01161bc
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
4 changes: 4 additions & 0 deletions intellij-haskell/META-INF/plugin.xml
Expand Up @@ -285,6 +285,10 @@
implementationClass="intellij.haskell.cabal.completion.CabalCompletionContributor"/>
<lookup.charFilter implementation="intellij.haskell.editor.HaskellCompletionCharFilter"/>
<psi.referenceContributor language="Haskell" implementation="intellij.haskell.psi.HaskellReferenceContributor"/>
<liveTemplateContext implementation="intellij.haskell.editor.HaskellTemplateContextType"/>
<liveTemplateContext implementation="intellij.haskell.editor.HaskellPragmaTemplateContextType"/>
<liveTemplateContext implementation="intellij.haskell.editor.HaskellGlobalDefinitionTemplateContextType"/>
<defaultLiveTemplatesProvider implementation="intellij.haskell.editor.HaskellLiveTemplateProvider"/>

<!-- Handler -->
<typedHandler implementation="intellij.haskell.editor.HaskellTypedHandler" id="HaskellFile"/>
Expand Down
78 changes: 78 additions & 0 deletions src/main/resources/liveTemplates/Haskell.xml
@@ -0,0 +1,78 @@
<templateSet group="Haskell">
<template name="language"
value="{-# LANGUAGE $END$ #-}"
description="LANGUAGE Pragma"
toReformat="true"
toShortenFQNames="true">
<context>
<option name="HASKELL_PRAGMA" value="true" />
</context>
</template>
<template name="include"
value="{-# INCLUDE $END$ #-}"
description="INLCUDE Pragma"
toReformat="true"
toShortenFQNames="true">
<context>
<option name="HASKELL_PRAGMA" value="true" />
</context>
</template>
<template name="optionsghc"
value="{-# GHC_OPTIONS $END$ #-}"
description="OPTIONS_GHC Pragma"
toReformat="true"
toShortenFQNames="true">
<context>
<option name="HASKELL_PRAGMA" value="true" />
</context>
</template>
<template name="instance"
value="instance $EXPR$ where&#10; $END$"
description="Type instance declaration"
toReformat="false"
toShortenFQNames="false">
<variable name="EXPR" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="HASKELL_GLOB_DEF" value="true" />
</context>
</template>
<template name="deriving"
value="deriving instance $EXPR$"
description="Standalone deriving declaration"
toReformat="true" toShortenFQNames="true">
<variable name="EXPR" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="HASKELL_GLOB_DEF" value="true" />
</context>
</template>
<template name="class"
value="class $EXPR$ where&#10; $END$"
description="Type class declaration"
toReformat="true"
toShortenFQNames="true">
<variable name="EXPR" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="HASKELL_GLOB_DEF" value="true" />
</context>
</template>
<template name="data"
value="data $NAME$ = $END$"
description="Data type declaration"
toReformat="false"
toShortenFQNames="true">
<variable name="NAME" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="HASKELL_GLOB_DEF" value="true" />
</context>
</template>
<template name="datag"
value="data $NAME$ where&#10; $END$"
description="Generalized data type declaration"
toReformat="false"
toShortenFQNames="true">
<variable name="NAME" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="HASKELL_GLOB_DEF" value="true" />
</context>
</template>
</templateSet>
45 changes: 45 additions & 0 deletions src/main/scala/intellij/haskell/editor/HaskellLiveTemplate.scala
@@ -0,0 +1,45 @@
package intellij.haskell.editor

import com.intellij.codeInsight.template.TemplateContextType
import com.intellij.codeInsight.template.impl.DefaultLiveTemplatesProvider
import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil
import intellij.haskell.HaskellFileType
import intellij.haskell.psi.{HaskellExpression, HaskellFileHeader, HaskellModuleBody}

class HaskellTemplateContextType extends TemplateContextType("HASKELL_FILE", "Haskell") {
override def isInContext(file: PsiFile, offset: Int): Boolean =
file.getFileType == HaskellFileType.Instance
}

class HaskellPragmaTemplateContextType extends TemplateContextType("HASKELL_PRAGMA", "Pragma", classOf[HaskellTemplateContextType]) {
override def isInContext(file: PsiFile, offset: Int): Boolean = {
if (file.getFileType != HaskellFileType.Instance) return false
var element = file.findElementAt(offset)
if (element == null) element = file.findElementAt(offset - 1)
if (element == null) return false
PsiTreeUtil.getParentOfType(element, classOf[HaskellFileHeader]) != null
}
}

class HaskellGlobalDefinitionTemplateContextType extends TemplateContextType("HASKELL_GLOB_DEF", "Global definition", classOf[HaskellTemplateContextType]) {
override def isInContext(file: PsiFile, offset: Int): Boolean = {
if (file.getFileType != HaskellFileType.Instance) return false
var element = file.findElementAt(offset)
if (element == null) element = file.findElementAt(offset - 1)
if (element == null) return false
PsiTreeUtil.getParentOfType(element, classOf[HaskellModuleBody]) != null &&
PsiTreeUtil.getParentOfType(element, classOf[HaskellExpression]) == null
}
}

object DefaultHolder {
val DEFAULT = Array("liveTemplates/Haskell")
}

class HaskellLiveTemplateProvider extends DefaultLiveTemplatesProvider {
override def getDefaultLiveTemplateFiles: Array[String] = DefaultHolder.DEFAULT

override def getHiddenLiveTemplateFiles: Array[String] = null
}

0 comments on commit 01161bc

Please sign in to comment.