-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change to a LanguageConsole to restore command history.
It's a bit of a kludge: the language is 'Plain Text'. But this seems like the path of least resistance. No autocompletion provided: that's a much bigger job. Requires IDEA 11 (Nika), so bumping the version to 1.4.0-SNAPSHOT and branching until I release 1.3.2 from master. Fixes #42, References #33
- Loading branch information
Showing
5 changed files
with
131 additions
and
1 deletion.
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
84 changes: 84 additions & 0 deletions
84
sbt-plugin/src/main/java/net/orfjackal/sbt/plugin/sbtlang/SbtCompletionContributor.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,84 @@ | ||
// Copyright © 2010-2011, Esko Luontola <www.orfjackal.net> | ||
// This software is released under the Apache License 2.0. | ||
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package net.orfjackal.sbt.plugin.sbtlang; | ||
|
||
import com.intellij.codeInsight.completion.*; | ||
import com.intellij.codeInsight.lookup.LookupElement; | ||
import com.intellij.codeInsight.lookup.LookupElementBuilder; | ||
import com.intellij.lang.ASTNode; | ||
import com.intellij.lang.LanguageWordCompletion; | ||
import com.intellij.openapi.project.DumbService; | ||
import com.intellij.patterns.PlatformPatterns; | ||
import com.intellij.psi.PlainTextTokenTypes; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.PsiReference; | ||
import com.intellij.psi.tree.IElementType; | ||
import com.intellij.util.ProcessingContext; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.*; | ||
|
||
import static com.intellij.patterns.StandardPatterns.character; | ||
|
||
// TODO get completion variants from SBT over a telnet-like interface | ||
// TODO target this contributor to the SBT console in a more principled manner (rather than using the getContainingFile.getName == SbtLanguage.INSTACNCE.getID) | ||
|
||
// Adapted from WordCompletionContributor in IDEA core. | ||
public class SbtCompletionContributor extends CompletionContributor { | ||
|
||
@Override | ||
public void fillCompletionVariants(final CompletionParameters parameters, final CompletionResultSet result) { | ||
if (parameters.getCompletionType() == CompletionType.BASIC && shouldPerformWordCompletion(parameters)) { | ||
addWordCompletionVariants(result, parameters, Collections.<String>emptySet()); | ||
} | ||
} | ||
|
||
public static void addWordCompletionVariants(CompletionResultSet result, CompletionParameters parameters, Set<String> excludes) { | ||
int startOffset = parameters.getOffset(); | ||
PsiElement insertedElement = parameters.getPosition(); | ||
final CompletionResultSet plainResultSet = result.withPrefixMatcher(CompletionUtil.findIdentifierPrefix(insertedElement, | ||
startOffset, | ||
character().javaIdentifierPart().andNot(character().equalTo('$')), | ||
character().javaIdentifierStart())); | ||
List<String> strings = Arrays.asList("compile", "run", "test:compile", "test", "project", "projects", "inspect", "show", "last", "last-grep"); | ||
for (String string : strings) { | ||
final LookupElement item = LookupElementBuilder.create(string); | ||
plainResultSet.addElement(item); | ||
} | ||
} | ||
|
||
private static boolean shouldPerformWordCompletion(CompletionParameters parameters) { | ||
final PsiElement insertedElement = parameters.getPosition(); | ||
final boolean dumb = DumbService.getInstance(insertedElement.getProject()).isDumb(); | ||
if (dumb) { | ||
return true; | ||
} | ||
// TODO Hackedy-hack. | ||
if (!insertedElement.getContainingFile().getName().equals("SBT")) { | ||
return false; | ||
} | ||
|
||
if (parameters.getInvocationCount() == 0) { | ||
return false; | ||
} | ||
|
||
final PsiFile file = insertedElement.getContainingFile(); | ||
|
||
final int startOffset = parameters.getOffset(); | ||
|
||
final PsiElement element = file.findElementAt(startOffset - 1); | ||
|
||
ASTNode textContainer = element != null ? element.getNode() : null; | ||
while (textContainer != null) { | ||
final IElementType elementType = textContainer.getElementType(); | ||
if (LanguageWordCompletion.INSTANCE.isEnabledIn(elementType) || elementType == PlainTextTokenTypes.PLAIN_TEXT) { | ||
return true; | ||
} | ||
textContainer = textContainer.getTreeParent(); | ||
} | ||
return false; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
sbt-plugin/src/main/java/net/orfjackal/sbt/plugin/sbtlang/SbtFileViewProviderFactory.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,22 @@ | ||
// Copyright © 2010-2011, Esko Luontola <www.orfjackal.net> | ||
// This software is released under the Apache License 2.0. | ||
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package net.orfjackal.sbt.plugin.sbtlang; | ||
|
||
import com.intellij.lang.Language; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.psi.FileViewProvider; | ||
import com.intellij.psi.FileViewProviderFactory; | ||
import com.intellij.psi.PsiManager; | ||
import com.intellij.psi.SingleRootFileViewProvider; | ||
|
||
public class SbtFileViewProviderFactory implements FileViewProviderFactory { | ||
|
||
public FileViewProvider createFileViewProvider(VirtualFile file, Language language, PsiManager manager, boolean physical) { | ||
return new SingleRootFileViewProvider(manager, file, physical, language.getBaseLanguage()) { | ||
|
||
|
||
}; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
sbt-plugin/src/main/java/net/orfjackal/sbt/plugin/sbtlang/SbtLanguage.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,19 @@ | ||
// Copyright © 2010-2011, Esko Luontola <www.orfjackal.net> | ||
// This software is released under the Apache License 2.0. | ||
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package net.orfjackal.sbt.plugin.sbtlang; | ||
|
||
|
||
import com.intellij.lang.Language; | ||
import com.intellij.lang.StdLanguages; | ||
import org.jetbrains.annotations.NonNls; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** A language for input in the SBT console */ | ||
public class SbtLanguage extends Language { | ||
public SbtLanguage() { | ||
super(StdLanguages.TEXT, "SBT"); | ||
} | ||
public static final Language INSTANCE = new SbtLanguage(); | ||
} |
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
111587c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool!