Skip to content

Commit

Permalink
Live template tutorial pt. 2
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed Jan 12, 2016
1 parent 026d72a commit 6b7f0dc
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/runConfigurations/editor_basics.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/runConfigurations/facet_basics.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/runConfigurations/framework.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/runConfigurations/kotlin_demo.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/runConfigurations/project_model.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/runConfigurations/register_actions.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/runConfigurations/simple_language_plugin.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/runConfigurations/tree_structure_provider.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions _SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@
* [Supporting Module Types](tutorials/project_wizard/module_types.md)
* [Code Inspections](tutorials/code_inspections.md)
* [Code Intentions](tutorials/code_intentions.md)
* [Live Templates](tutorials/live_templates.md)
* [1. Adding Live Template Support](tutorials/live_templates/template_support.md)\
* [Run Configurations](tutorials/run_configurations.md)
* [Supporting Frameworks](tutorials/framework.md)
* [Tree Structure View](tutorials/tree_structure_view.md)
Expand Down
8 changes: 5 additions & 3 deletions code_samples/live_templates/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<idea-plugin version="2">
<id>com.your.company.unique.plugin.id</id>
<name>Plugin display name here</name>
<id>live_templates</id>
<name>live_templates</name>
<version>1.0</version>
<vendor email="[email protected]" url="http://www.yourcompany.com">YourCompany</vendor>

Expand All @@ -25,7 +25,9 @@
-->

<extensions defaultExtensionNs="com.intellij">
<!-- Add your extensions here -->
<defaultLiveTemplatesProvider implementation="MarkdownTemplateProvider"/>
<liveTemplateContext implementation="MarkdownContext"/>

</extensions>

<actions>
Expand Down
16 changes: 13 additions & 3 deletions code_samples/live_templates/src/MarkdownContext.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import com.intellij.codeInsight.template.EverywhereContextType;
import com.intellij.codeInsight.template.TemplateContextType;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Created by breandan on 1/11/2016.
*/
public class MarkdownContext extends TemplateContextType {
protected MarkdownContext() {
super("MARKDOWN", "Markdown");
}

@Override
public boolean isInContext(@NotNull PsiFile file, int offset) {
return file.getName().endsWith(".md");
}
}
15 changes: 12 additions & 3 deletions code_samples/live_templates/src/MarkdownTemplateProvider.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import com.intellij.codeInsight.template.impl.DefaultLiveTemplatesProvider;
import org.jetbrains.annotations.Nullable;

/**
* Created by breandan on 1/11/2016.
*/
public class MarkdownTemplateProvider implements DefaultLiveTemplatesProvider {
@Override
public String[] getDefaultLiveTemplateFiles()
{
return new String[] {"liveTemplates/Markdown"};
}

@Nullable
@Override
public String[] getHiddenLiveTemplateFiles() {
return new String[0];
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<idea-plugin version="2">
<name>Plugin name here</name>
<name>max_opened_projects</name>
<description>short description of the plugin</description>
<version>1.0</version>
<vendor>YourCompany</vendor>
Expand Down
3 changes: 2 additions & 1 deletion tutorials.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ A more detailed explanation of the API components can be found in the
* [Kotlin for Plugin Developers](tutorials/kotlin.md)
* [Gradle Build System](tutorials/build_system.md)
* [Code Inspections](tutorials/code_inspections.md)
* [Code Intentions](tutorials/code_intentions.md)
* [Code Intentions](tutorials/code_intentions.md)
* [Live Templates](tutorials/live_templates.md)
15 changes: 15 additions & 0 deletions tutorials/live_templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: Live Templates
---

*Live templates* in IntelliJ IDEA are customizable rules that allow developers to abbreviate repetitive patterns of text in the editor. When a user types the designated abbreviation followed by a configurable *expansion key* (usually `Tab`), the IDE will transform the preceding input sequence to its full-length output, and update the cursor position. For example, consider a `for` loop. Typically, the end user would need to type `for(int i = 0; i < 10; i++) {<Enter><Tab><Enter><Enter>}<Up>`. This pattern may be shortened to `fori<Tab>` and the remaining contents will be expanded, leaving the following structure:

```
for(int i = [|]; i < []; i++) {
[]
}
```

As the user completes each section of the `for` loop and presses `Tab`, the cursor will advance to the next position in the editor. For more information about creating your own Custom Live Templates, you may refer to the [corresponding documentation](https://www.jetbrains.com/idea/help/creating-and-editing-live-templates.html). In this tutorial, we will illustrate how to add default Custom Live Templates to an IntelliJ Platform plugin, and assign valid contexts where these templates can take on added functionality based on the surrounding code and file type. We will discuss how to export existing live templates, and bundle them within a plugin to give plugin users added typing efficiency when using a custom language.

* [1. Create a new Live Template](live_templates/template_support.md)
31 changes: 31 additions & 0 deletions tutorials/live_templates/template_support.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,34 @@ Copy this file into your plugin's resources, (eg. `project/resource/liveTemplate

## Implement DefaultLiveTemplatesProvider

The `DefaultLiveTemplatesProvider` tells us where to find the Live Template settings file. Make sure to include the full path to the file, relative to the resources directory, excluding the file name.

```java
{% include /code_samples/live_templates/src/MarkdownTemplateProvider.java %}
```

## Implement TemplateContextType

A `TemplateContextType` tells us where the live template is applicible.

```java
{% include /code_samples/live_templates/src/MarkdownTemplateProvider.java %}
```

Once you define the `TemplateContextType`, be sure to add the assigned context type to the previously created Live Template settings file. Under `<template>...</template>` add the following context:

```xml
<context>
<option name="MARKDOWN" value=true />
</context>
```

## Register Extension Points

```xml
{% include /code_samples/live_templates/resources/META-INF/plugin.xml %}
```

## Check Plugin

Now check that the plugin is working correctly. Run the plugin and verify there is a new entry under *File \| Settings \| Live Templates \| Markdown \| \[*. Finally, create a new file `Test.md` and confirm that the Live Template works.

0 comments on commit 6b7f0dc

Please sign in to comment.