Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add live template contexts and some default live templates
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 $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 $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 $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
45
src/main/scala/intellij/haskell/editor/HaskellLiveTemplate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|