Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding live templates for OCaml #343

Merged
merged 7 commits into from
Sep 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@
<liveTemplateContext implementation="com.reason.ide.template.RmlContextType$Generic"/>
<defaultLiveTemplates file="liveTemplates/Reason.xml"/>

<liveTemplateContext implementation="com.reason.ide.template.OCamlContentType"/>
<defaultLiveTemplates file="liveTemplates/OCaml.xml"/>

<!--
| Intentions
-->
Expand Down
129 changes: 129 additions & 0 deletions resources/liveTemplates/OCaml-doc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Documentation of OCaml Live templates

**Scope**: `*.ml` and `*.mli`.

- `begin`

```ocaml
begin

end
```

- `class`

```ocaml
class name =
object (self)
contents
end
```

- `for`

```ocaml
for i = starval to endval do
code
done
```

- `fun`

```ocaml
(fun () -> body)
```

- `func`

```ocaml
(function
| patt1 -> expr1
| patt2 -> expr2)
```

- `if`

```ocaml
if (cond) then expr else expr2
```

- `let`

```ocaml
let var(s) = expr
```

- `lin`

```ocaml
let var(s) = expr in expr2
```

- `match`

```ocaml
match value with
| patt -> expr
| _ -> expr2
```

- `method`

```ocaml
method name =
```

- `msig`

```ocaml
module Name : sig

end
```

- `mstruct`

```ocaml
module Name = struct

end
```

- `mtype`

```ocaml
module type Name = sig

end
```

- `thread`

```ocaml
ignore (Thread.create (fun () ->

) ())
```

- `try`

```ocaml
try

with
| _ -> failwith "Unknown"
```

- `type`

```ocaml
type name = expr
```

- `while`

```ocaml
while condition do

done
```
128 changes: 128 additions & 0 deletions resources/liveTemplates/OCaml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<templateSet group="OCaml">
<template name="while" value="while $condition$ do&#10;&#9;$code$&#10;done" description="while loop" toReformat="false" toShortenFQNames="true">
<variable name="condition" expression="" defaultValue="&quot;condition&quot;" alwaysStopAt="true" />
<variable name="code" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="type" value="type $name$ = $value$" description="type" toReformat="false" toShortenFQNames="true">
<variable name="name" expression="" defaultValue="&quot;name&quot;" alwaysStopAt="true" />
<variable name="value" expression="" defaultValue="&quot;expr&quot;" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="try" value="try&#10;&#9;$code$&#10;with&#10;| _ -&gt; failwith &quot;Unknown&quot;" description="try" toReformat="false" toShortenFQNames="true">
<variable name="code" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="thread" value="ignore (Thread.create (fun () -&gt; &#10; $code$&#10; ) ())" description="create thread" toReformat="false" toShortenFQNames="true">
<variable name="code" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="mtype" value="module type $Name$ = sig&#10;&#9;$code$&#10;end" description="module type" toReformat="false" toShortenFQNames="true">
<variable name="Name" expression="" defaultValue="&quot;Name&quot;" alwaysStopAt="true" />
<variable name="code" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="msig" value="module $Name$ : sig&#10;&#9;$code$&#10;end" description="module signature" toReformat="false" toShortenFQNames="true">
<variable name="Name" expression="" defaultValue="&quot;Name&quot;" alwaysStopAt="true" />
<variable name="code" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="mstruct" value="module $Name$ = struct&#10;&#9;$code$&#10;end" description="module struct" toReformat="false" toShortenFQNames="true">
<variable name="Name" expression="" defaultValue="&quot;Name&quot;" alwaysStopAt="true" />
<variable name="code" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="method" value="method $name$ = $code$" description="method" toReformat="false" toShortenFQNames="true">
<variable name="name" expression="" defaultValue="&quot;name&quot;" alwaysStopAt="true" />
<variable name="code" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="match" value="match $value$ with&#10;| $patt$ -&gt; $expr$&#10;| $_$ -&gt; $expr2$" description="match" toReformat="false" toShortenFQNames="true">
<variable name="value" expression="" defaultValue="&quot;value&quot;" alwaysStopAt="true" />
<variable name="patt" expression="" defaultValue="&quot;patt&quot;" alwaysStopAt="true" />
<variable name="expr" expression="" defaultValue="&quot;expr&quot;" alwaysStopAt="true" />
<variable name="_" expression="" defaultValue="&quot;_&quot;" alwaysStopAt="true" />
<variable name="expr2" expression="" defaultValue="&quot;expr2&quot;" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="lin" value="let $var$ = $expr$ in $expr2$" description="let in" toReformat="false" toShortenFQNames="true">
<variable name="var" expression="" defaultValue="&quot;var(s)&quot;" alwaysStopAt="true" />
<variable name="expr" expression="" defaultValue="&quot;expr&quot;" alwaysStopAt="true" />
<variable name="expr2" expression="" defaultValue="&quot;expr2&quot;" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="fun" value="(fun $p$ -&gt; $body$)" description="fun" toReformat="false" toShortenFQNames="true">
<variable name="p" expression="" defaultValue="&quot;()&quot;" alwaysStopAt="true" />
<variable name="body" expression="" defaultValue="&quot;body&quot;" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="func" value="(function&#10;| $patt1$ -&gt; $expr1$&#10;| $patt2$ -&gt; $expr2$)" description="function alt" toReformat="false" toShortenFQNames="true">
<variable name="patt1" expression="" defaultValue="&quot;patt1&quot;" alwaysStopAt="true" />
<variable name="expr1" expression="" defaultValue="&quot;expr1&quot;" alwaysStopAt="true" />
<variable name="patt2" expression="" defaultValue="&quot;patt2&quot;" alwaysStopAt="true" />
<variable name="expr2" expression="" defaultValue="&quot;expr2&quot;" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="if" value="if ($cond$) then $expr$ else $expr2$" description="if" toReformat="false" toShortenFQNames="true">
<variable name="cond" expression="" defaultValue="&quot;cond&quot;" alwaysStopAt="true" />
<variable name="expr" expression="" defaultValue="&quot;expr&quot;" alwaysStopAt="true" />
<variable name="expr2" expression="" defaultValue="&quot;expr2&quot;" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="let" value="let $var$ = $expr$" description="let" toReformat="false" toShortenFQNames="true">
<variable name="var" expression="" defaultValue="&quot;var(s)&quot;" alwaysStopAt="true" />
<variable name="expr" expression="" defaultValue="&quot;expr&quot;" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="class" value="class $name$ =&#10;&#9;object $self$&#10;&#9;&#9;$contents$&#10;&#9;end" description="class" toReformat="false" toShortenFQNames="true">
<variable name="name" expression="" defaultValue="&quot;name&quot;" alwaysStopAt="true" />
<variable name="self" expression="" defaultValue="&quot;(self)&quot;" alwaysStopAt="true" />
<variable name="contents" expression="" defaultValue="&quot;contents&quot;" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="begin" value="begin&#10;&#9;$code$&#10;end" description="begin" toReformat="false" toShortenFQNames="true">
<variable name="code" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
<template name="for" value="for $i$ = $startval$ to $endval$ do&#10;&#9;$code$&#10;done" description="for loop" toReformat="false" toShortenFQNames="true">
<variable name="i" expression="" defaultValue="&quot;i&quot;" alwaysStopAt="true" />
<variable name="startval" expression="" defaultValue="&quot;starval&quot;" alwaysStopAt="true" />
<variable name="endval" expression="" defaultValue="&quot;endval&quot;" alwaysStopAt="true" />
<variable name="code" expression="" defaultValue="&quot;code&quot;" alwaysStopAt="true" />
<context>
<option name="OCAML" value="true" />
</context>
</template>
</templateSet>
21 changes: 21 additions & 0 deletions src/com/reason/ide/template/OCamlContentType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.reason.ide.template;

import com.intellij.codeInsight.template.TemplateActionContext;
import com.intellij.codeInsight.template.TemplateContextType;
import org.jetbrains.annotations.NotNull;

/**
* Every "*.ml" kind of file is considered to be a part of the OCaml context
*/
public class OCamlContentType extends TemplateContextType {

protected OCamlContentType() {
super("OCAML", "Ocaml");
}

@Override
public boolean isInContext(@NotNull TemplateActionContext templateActionContext) {
final String name = templateActionContext.getFile().getName();
return name.endsWith(".ml") || name.endsWith(".mli");
}
}
2 changes: 1 addition & 1 deletion website/docs/contributing/plugin-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Follow the steps below to get the plugin running locally for development.
### Prepare your Environment

1. Install the [plugin prerequisites](http://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/prerequisites.html), ensuring that the Plugin DevKit plugin is enabled and [PsiViewer](https://plugins.jetbrains.com/plugin/227-psiviewer) is installed and enabled.
2. Clone the project `git clone https://github.com/JetBrains/intellij-community.git`
2. Clone the project `git clone https://github.com/giraud/reasonml-idea-plugin.git`
3. Import the project into Intellij as a Gradle project. This should auto-configure everything else needed to run the plugin.
4. If prompted, download the Intellij SDK source files. This is useful for debugging the Intellij platform code.

Expand Down