Skip to content

Commit

Permalink
Add brace matching/folding feature
Browse files Browse the repository at this point in the history
  • Loading branch information
justint committed May 25, 2019
1 parent 63b504e commit 1bf6814
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 36 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -20,6 +20,8 @@ _NOTE_: This plugin is in active development and features are still being implem
## Features

- `.usd`/`.usda` filetype syntax highlighting & validation

- Brace matching/folding

TODO:

Expand All @@ -31,8 +33,6 @@ TODO:

- Line markers for overrides, inheritance, etc

- Brace matching

- Python autocompletions for the `pxr.Usd` library

## License
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'com.justint'
version '0.1.3'
version '0.2'

sourceSets.main.java.srcDirs = ['src/main/gen','src/main/java']

Expand Down

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

9 changes: 0 additions & 9 deletions src/main/gen/com/justint/usdidea/lang/psi/usdPrimSpec.java

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

@@ -0,0 +1,68 @@
package com.justint.usdidea.codeinsight.folding;

import com.intellij.lang.ASTNode;
import com.intellij.lang.folding.FoldingBuilder;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.lang.folding.NamedFoldingDescriptor;
import com.intellij.openapi.editor.Document;
import com.intellij.psi.tree.IElementType;
import com.justint.usdidea.lang.psi.USDTypes;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class USDFoldingBuilder implements FoldingBuilder {

private static final IElementType[] BRACKET_TYPES = new IElementType[] {
USDTypes.BODY,
USDTypes.DICT,
USDTypes.TIME_SAMPLE
};

@NotNull
@Override
public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode astNode, @NotNull Document document) {
List<FoldingDescriptor> descriptors = new ArrayList<>();
collectRegionsRecursively(astNode, document, descriptors);
return descriptors.toArray(FoldingDescriptor.EMPTY);
}

private void collectRegionsRecursively(@NotNull final ASTNode astNode,
@NotNull final Document document,
@NotNull List<FoldingDescriptor> descriptors) {
final IElementType elementType = astNode.getElementType();

if (elementType == USDTypes.METADATA) {
descriptors.add(new NamedFoldingDescriptor(
astNode,
astNode.getTextRange(),
null,
"(...)"
));
} else if (Arrays.asList(BRACKET_TYPES).contains(elementType)) {
descriptors.add(new NamedFoldingDescriptor(
astNode,
astNode.getTextRange(),
null,
"{...}"));
}

for (ASTNode child : astNode.getChildren(null)) {
collectRegionsRecursively(child, document, descriptors);
}
}

@Nullable
@Override
public String getPlaceholderText(@NotNull ASTNode astNode) {
return "...";
}

@Override
public boolean isCollapsedByDefault(@NotNull ASTNode astNode) {
return false;
}
}
@@ -0,0 +1,35 @@
package com.justint.usdidea.codeinsight.highlighting;

import com.intellij.lang.BracePair;
import com.intellij.lang.PairedBraceMatcher;
import com.intellij.psi.PsiFile;
import com.intellij.psi.tree.IElementType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.justint.usdidea.lang.psi.USDTypes.*;

public class USDBraceMatcher implements PairedBraceMatcher {

private static final BracePair[] PAIRS = new BracePair[] {
new BracePair(LEFTBRACE, RIGHTBRACE, true),
new BracePair(LEFTPARENS, RIGHTPARENS, true),
new BracePair(LEFTBRACKET, RIGHTBRACKET, false)
};

@NotNull
@Override
public BracePair[] getPairs() {
return PAIRS;
}

@Override
public boolean isPairedBracesAllowedBeforeType(@NotNull IElementType lBraceType, @Nullable IElementType contextType) {
return true;
}

@Override
public int getCodeConstructStart(PsiFile file, int openingBraceOffset) {
return openingBraceOffset;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/justint/usdidea/lang/USD.bnf
Expand Up @@ -174,7 +174,7 @@ Identifier ::= alpha (alpha | number)*
NamespacedIdentifier ::= Identifier (colon Identifier)+


PrimSpec ::= Specifier Metadata? Body {methods=[getPrimName getPrimType getName getPresentation]}
PrimSpec ::= Specifier Metadata? Body

Specifier ::= def [Typename] PrimName | over PrimName | class [Typename] PrimName

Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/META-INF/change-notes.html
@@ -1,6 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<body>
<strong>Version 0.2:</strong>
<ul>
<li>Added brace matching and block folding features</li>
<li>Improved syntax highlighting (Prim names, special metadata keywords)</li>
<li>Set a wider release version support for IDE versions 2016-2019 (builds 145-191.*)</li>
</ul>
<strong>Version 0.1:</strong>
<ul>
<li>Basic syntax highlighting</li>
Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Expand Up @@ -12,12 +12,12 @@
<ul>
<li>Syntax highlighting</li>
<li>Parser</li>
<li>Brace matching & block folding</li>
</ul>
<strong>Planned features:</strong>
<ul>
<li>Reference Contributor implementation (Go to declaration feature)</li>
<li>Structure View</li>
<li>Brace matching</li>
<li>Line markers for <i>overrides</i>, <i>inherits</i>, and more</li>
</ul>
]]></description>
Expand All @@ -35,7 +35,9 @@
<!-- TODO: re-add the color settings page once syntax highlighting is more complete -->
<!--<colorSettingsPage implementation="com.justint.usdidea.codeinsight.highlighting.USDColorSettingsPage"/>-->

<lang.psiStructureViewFactory language="USD" implementationClass="com.justint.usdidea.codeinsight.structureview.USDStructureViewFactory"/>
<lang.foldingBuilder language="USD" implementationClass="com.justint.usdidea.codeinsight.folding.USDFoldingBuilder"/>
<lang.braceMatcher language="USD" implementationClass="com.justint.usdidea.codeinsight.highlighting.USDBraceMatcher"/>

</extensions>

<actions>
Expand Down

0 comments on commit 1bf6814

Please sign in to comment.