Skip to content

Commit

Permalink
Add the commenter and live templates to FSH
Browse files Browse the repository at this point in the history
  • Loading branch information
qligier committed Sep 22, 2024
1 parent 4665a6b commit c708fe6
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2024 Quentin Ligier. Use of this source code is governed by the MIT license.

package ch.qligier.jetbrains.plugin.fss.fsh;

import com.intellij.lang.Commenter;
import org.jspecify.annotations.Nullable;

/**
* Defines the support for "Comment with Line Comment" and "Comment with Block Comment" actions in the FSH language.
*
* @author Quentin Ligier
* @see <a href="https://plugins.jetbrains.com/docs/intellij/additional-minor-features.html#comment-code">Comment
* Code</a>
* @see <a href="https://plugins.jetbrains.com/docs/intellij/commenter.html">Commenter</a>
**/
public class FshCommenter implements Commenter {

public static final String LINE_COMMENT_PREFIX = "//";
public static final String BLOCK_COMMENT_PREFIX = "/*";
public static final String BLOCK_COMMENT_SUFFIX = "*/";

public FshCommenter() {
super();
System.out.println("FshCommenter built");
}

/**
* Returns the string that prefixes a line comment in the language, or null if the language doesn't support line
* comments. If the language supports several prefixes for line comments, only one of them (the most recommended to
* use) is returned. Use {@link #getLineCommentPrefixes()} to get all supported line comment prefixes.
*
* @return the line comment text, or null.
*/
@Override
public @Nullable String getLineCommentPrefix() {
System.out.println("FshCommenter getLineCommentPrefix");
return LINE_COMMENT_PREFIX;
}

/**
* Returns the string which marks the beginning of a block comment in the language, or null if the language doesn't
* support block comments.
*
* @return the block comment start text, or null.
*/
@Override
public @Nullable String getBlockCommentPrefix() {
System.out.println("FshCommenter getBlockCommentPrefix");
return BLOCK_COMMENT_PREFIX;
}

/**
* Returns the string which marks the end of a block comment in the language, or null if the language doesn't
* support block comments.
*
* @return the block comment end text, or null.
*/
@Override
public @Nullable String getBlockCommentSuffix() {
System.out.println("FshCommenter getBlockCommentSuffix");
return BLOCK_COMMENT_SUFFIX;
}

/**
* Returns the string which marks the commented beginning of a block comment in the language, or null if the
* language doesn't support block comments.
*
* @return the commented block comment start text, or null.
*/
@Override
public @Nullable String getCommentedBlockCommentPrefix() {
System.out.println("FshCommenter getCommentedBlockCommentPrefix");
return null;
}

/**
* Returns the string which marks the commented end of a block comment in the language, or null if the language
* doesn't support block comments.
*
* @return the commented block comment end text, or null.
*/
@Override
public @Nullable String getCommentedBlockCommentSuffix() {
System.out.println("FshCommenter getCommentedBlockCommentSuffix");
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Quentin Ligier. Use of this source code is governed by the MIT license.

package ch.qligier.jetbrains.plugin.fss.fsh;

import com.intellij.codeInsight.template.TemplateActionContext;
import com.intellij.codeInsight.template.TemplateContextType;
import org.jspecify.annotations.NonNull;

/**
* A TemplateContextType tells the IntelliJ Platform where the Live Template is applicable: FSH files.
*
* @author Quentin Ligier
* @see <a
* href="https://plugins.jetbrains.com/docs/intellij/template-support.html#implement-templatecontexttype">Implement
* TemplateContextType</a>
**/
public class FshTemplateContextType extends TemplateContextType {

protected FshTemplateContextType() {
super("FSH");
}

@Override
public boolean isInContext(@NonNull final TemplateActionContext templateActionContext) {
return templateActionContext.getFile().getName().endsWith(".fsh");
}
}
7 changes: 7 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
<lang.syntaxHighlighterFactory
language="FSH"
implementationClass="org.jetbrains.plugins.textmate.language.syntax.highlighting.TextMateSyntaxHighlighterFactory"/>
<lang.commenter language="FSH"
implementationClass="ch.qligier.jetbrains.plugin.fss.fsh.FshCommenter"/>

<defaultLiveTemplates file="/fsh/liveTemplates.xml"/>
<liveTemplateContext
contextId="FSH"
implementation="ch.qligier.jetbrains.plugin.fss.fsh.FshTemplateContextType"/>
</extensions>

<applicationListeners>
Expand Down
35 changes: 35 additions & 0 deletions src/main/resources/fsh/liveTemplates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!--
~ Copyright 2024 Quentin Ligier. Use of this source code is governed by the MIT license.
-->
<templateSet group="FSH">
<template name="profile"
value="Profile: $NAME$&#10;Parent: $PARENT$&#10;Id: $ID$&#10;Title: &quot;$TITLE$&quot;&#10;Description: &quot;$DESCRIPTION$&quot;&#10;* $RULE$"
description="Create a new Profile"
toReformat="false"
toShortenFQNames="true">
<variable name="NAME" expression="" defaultValue="" alwaysStopAt="true"/>
<variable name="PARENT" expression="" defaultValue="" alwaysStopAt="true"/>
<variable name="ID"
expression="regularExpression(lowercaseAndDash(NAME), &quot;_&quot;, &quot;-&quot;)"
defaultValue=""
alwaysStopAt="true"/>
<variable name="TITLE" expression="NAME" defaultValue="" alwaysStopAt="true"/>
<variable name="DESCRIPTION" expression="" defaultValue="" alwaysStopAt="true"/>
<variable name="RULE" expression="" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="FSH" value="true"/>
</context>
</template>

<template name="alias"
value="Alias: $$$NAME$ = $URN$"
description="Create a new Alias"
toReformat="false"
toShortenFQNames="true">
<variable name="NAME" expression="" defaultValue="" alwaysStopAt="true"/>
<variable name="URN" expression="" defaultValue="" alwaysStopAt="true"/>
<context>
<option name="FSH" value="true"/>
</context>
</template>
</templateSet>

0 comments on commit c708fe6

Please sign in to comment.