-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from ingwarsw/add-commenter
Add extra features to plugin
- Loading branch information
Showing
31 changed files
with
1,176 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
on: | ||
pull_request: | ||
push: | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
env: | ||
FORCE_COLOR: 1 | ||
EARTHLY_CONVERSION_PARALLELISM: "5" | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: earthly/actions/setup-earthly@v1 | ||
- name: test | ||
run: earthly +test |
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
37 changes: 37 additions & 0 deletions
37
src/main/java/dev/earthly/plugin/language/syntax/EarthlyCompletionContributor.java
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,37 @@ | ||
package dev.earthly.plugin.language.syntax; | ||
|
||
import com.intellij.codeInsight.completion.*; | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.patterns.PlatformPatterns; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.util.ProcessingContext; | ||
import dev.earthly.plugin.language.syntax.psi.EarthlyPsiElement; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
public class EarthlyCompletionContributor extends CompletionContributor { | ||
EarthlyCompletionContributor() { | ||
extend(CompletionType.BASIC, PlatformPatterns.psiElement().withParent(EarthlyPsiElement.class), | ||
new CompletionProvider<>() { | ||
public void addCompletions(@NotNull CompletionParameters parameters, | ||
@NotNull ProcessingContext context, | ||
@NotNull CompletionResultSet resultSet) { | ||
final PsiElement element = parameters.getPosition().getParent(); | ||
final Project project = element.getProject(); | ||
|
||
String key = element.getText().replace(CompletionUtilCore.DUMMY_IDENTIFIER_TRIMMED, ""); | ||
if (StringUtils.isEmpty(key)) | ||
return; | ||
List<PsiElement> matched = EarthlyUtil.findAllFunctions(project, (EarthlyPsiElement) element); | ||
for (PsiElement potential : matched) { | ||
if (potential.getText().startsWith(key)) | ||
resultSet.addElement(LookupElementBuilder.create(potential.getText())); | ||
} | ||
} | ||
} | ||
); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/dev/earthly/plugin/language/syntax/EarthlyFindUsagesProvider.java
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,62 @@ | ||
package dev.earthly.plugin.language.syntax; | ||
|
||
import com.intellij.lang.cacheBuilder.DefaultWordsScanner; | ||
import com.intellij.lang.cacheBuilder.WordsScanner; | ||
import com.intellij.lang.findUsages.FindUsagesProvider; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.tree.TokenSet; | ||
import dev.earthly.plugin.language.syntax.highlighting.EarthlyHighlightingLexer; | ||
import dev.earthly.plugin.language.syntax.highlighting.EarthlyTokenSets; | ||
import dev.earthly.plugin.language.syntax.psi.EarthlyPsiElement; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class EarthlyFindUsagesProvider implements FindUsagesProvider { | ||
|
||
@Override | ||
public WordsScanner getWordsScanner() { | ||
return new DefaultWordsScanner(new EarthlyHighlightingLexer(), | ||
EarthlyTokenSets.IDENTIFIERS, | ||
EarthlyTokenSets.COMMENTS, | ||
TokenSet.EMPTY); | ||
} | ||
|
||
@Override | ||
public boolean canFindUsagesFor(@NotNull PsiElement psiElement) { | ||
return psiElement instanceof EarthlyPsiElement; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public String getHelpId(@NotNull PsiElement psiElement) { | ||
return null; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getType(@NotNull PsiElement element) { | ||
if (element instanceof EarthlyPsiElement) { | ||
return "earthly target"; | ||
} | ||
return ""; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getDescriptiveName(@NotNull PsiElement element) { | ||
if (element instanceof EarthlyPsiElement) { | ||
return "Earthly: " + element.getText(); | ||
} | ||
return ""; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getNodeText(@NotNull PsiElement element, boolean useFullName) { | ||
if (element instanceof EarthlyPsiElement) { | ||
return element.getText(); | ||
} | ||
return ""; | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/dev/earthly/plugin/language/syntax/EarthlyLineMarkerProvider.java
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,35 @@ | ||
package dev.earthly.plugin.language.syntax; | ||
|
||
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo; | ||
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider; | ||
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.psi.PsiElement; | ||
import dev.earthly.plugin.language.syntax.psi.EarthlyPsiElement; | ||
import dev.earthly.plugin.metadata.EarthlyIcons; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
final class EarthlyLineMarkerProvider extends RelatedItemLineMarkerProvider { | ||
|
||
@Override | ||
protected void collectNavigationMarkers(@NotNull PsiElement element, | ||
@NotNull Collection<? super RelatedItemLineMarkerInfo<?>> result) { | ||
if (!(element instanceof EarthlyPsiElement eapsi)) { | ||
return; | ||
} | ||
|
||
Project project = element.getProject(); | ||
final List<PsiElement> functions = EarthlyUtil.findFunctionsByName(project, eapsi); | ||
|
||
if (!functions.isEmpty()) { | ||
NavigationGutterIconBuilder<PsiElement> builder = | ||
NavigationGutterIconBuilder.create(EarthlyIcons.FILE) | ||
.setTargets(functions) | ||
.setTooltipText("Navigate to Earthly language property"); | ||
result.add(builder.createLineMarkerInfo(element)); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/dev/earthly/plugin/language/syntax/EarthlyNamesValidator.java
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,28 @@ | ||
package dev.earthly.plugin.language.syntax; | ||
|
||
import com.intellij.lang.refactoring.NamesValidator; | ||
import com.intellij.openapi.project.Project; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
import java.util.regex.Pattern; | ||
|
||
public class EarthlyNamesValidator implements NamesValidator { | ||
private static final Predicate<String> TARGET_PATTERN = Pattern.compile("^[a-z][a-zA-Z0-9.\\-]*$").asMatchPredicate(); | ||
private static final Predicate<String> FUNCTION_PATTERN = Pattern.compile("^[A-Z][a-zA-Z0-9._]*$").asMatchPredicate(); | ||
@Override | ||
public boolean isKeyword(@NotNull String name, Project project) { | ||
List<String> keywords = List.of("base"); | ||
return keywords.contains(name); | ||
} | ||
|
||
@Override | ||
public boolean isIdentifier(@NotNull String name, Project project) { | ||
final int len = name.length(); | ||
if (len == 0) return false; | ||
if (isKeyword(name, project)) | ||
return false; | ||
return TARGET_PATTERN.test(name) || FUNCTION_PATTERN.test(name); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/dev/earthly/plugin/language/syntax/EarthlyRefactoringSupportProvider.java
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,14 @@ | ||
package dev.earthly.plugin.language.syntax; | ||
|
||
import com.intellij.lang.refactoring.RefactoringSupportProvider; | ||
import com.intellij.psi.PsiElement; | ||
import dev.earthly.plugin.language.syntax.psi.EarthlyPsiElement; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class EarthlyRefactoringSupportProvider extends RefactoringSupportProvider { | ||
@Override | ||
public boolean isMemberInplaceRenameAvailable(@NotNull PsiElement elementToRename, @Nullable PsiElement context) { | ||
return (elementToRename instanceof EarthlyPsiElement); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/dev/earthly/plugin/language/syntax/EarthlyReference.java
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,62 @@ | ||
package dev.earthly.plugin.language.syntax; | ||
|
||
import com.intellij.codeInsight.lookup.LookupElement; | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.util.TextRange; | ||
import com.intellij.psi.*; | ||
import com.intellij.util.IncorrectOperationException; | ||
import dev.earthly.plugin.language.syntax.psi.EarthlyPsiElement; | ||
import dev.earthly.plugin.metadata.EarthlyIcons; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
final class EarthlyReference extends PsiReferenceBase<EarthlyPsiElement> implements PsiPolyVariantReference { | ||
|
||
EarthlyReference(@NotNull EarthlyPsiElement element) { | ||
super(element, new TextRange(0, element.getTextLength())); | ||
} | ||
|
||
@Override | ||
public ResolveResult @NotNull [] multiResolve(boolean incompleteCode) { | ||
Project project = myElement.getProject(); | ||
final List<PsiElement> properties = EarthlyUtil.findFunctionsByName(project, myElement); | ||
List<ResolveResult> results = new ArrayList<>(); | ||
for (PsiElement property : properties) { | ||
if (property != myElement) | ||
results.add(new PsiElementResolveResult(property)); | ||
} | ||
return results.toArray(new ResolveResult[0]); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public PsiElement resolve() { | ||
ResolveResult[] resolveResults = multiResolve(false); | ||
return resolveResults.length == 1 ? resolveResults[0].getElement() : null; | ||
} | ||
|
||
@Override | ||
public Object @NotNull [] getVariants() { | ||
Project project = myElement.getProject(); | ||
final List<PsiElement> properties = EarthlyUtil.findFunctionsByName(project, myElement); | ||
List<LookupElement> variants = new ArrayList<>(); | ||
for (final PsiElement property : properties) { | ||
if (property != myElement) { | ||
variants.add(LookupElementBuilder | ||
.create(property).withIcon(EarthlyIcons.FILE) | ||
.withTypeText(property.getContainingFile().getName()) | ||
); | ||
} | ||
} | ||
return variants.toArray(); | ||
} | ||
|
||
@Override | ||
public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException { | ||
return myElement.setName(newElementName); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/dev/earthly/plugin/language/syntax/EarthlyReferenceContributor.java
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,23 @@ | ||
package dev.earthly.plugin.language.syntax; | ||
|
||
import com.intellij.psi.*; | ||
import com.intellij.util.ProcessingContext; | ||
import dev.earthly.plugin.language.syntax.psi.EarthlyPsiElement; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static com.intellij.patterns.PlatformPatterns.psiElement; | ||
|
||
final class EarthlyReferenceContributor extends PsiReferenceContributor { | ||
|
||
@Override | ||
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) { | ||
registrar.registerReferenceProvider(psiElement(EarthlyPsiElement.class), | ||
new PsiReferenceProvider() { | ||
@Override | ||
public PsiReference @NotNull [] getReferencesByElement(@NotNull PsiElement element, | ||
@NotNull ProcessingContext context) { | ||
return new PsiReference[]{new EarthlyReference((EarthlyPsiElement) element)}; | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.