Skip to content

Commit

Permalink
update instructions and fix nullpointer exception
Browse files Browse the repository at this point in the history
  • Loading branch information
wodka committed Oct 30, 2014
1 parent 537e486 commit 6fceaed
Show file tree
Hide file tree
Showing 45 changed files with 2,117 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
*.class

*~
# Package Files #
*.jar
*.war
*.ear

*.DS_Store
*.DS_Store
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
phpspec plugin for PhpStorm IDE
=======================
===

http://projectspace.pl/plugins/phpspec
> be aware that building & testing is kind of annoying...
> If you find a solution that includes testing without
> restarting the ide, please tell me
Building / Development:
---

1. Setup IDEA for Plugin Development: http://confluence.jetbrains.com/display/IntelliJIDEA/Prerequisites
2. Add all entries from lib
3. set the libs aspectjrt-1.7.3.jar and gson-2.0.jar to be exported and compliled.
4. set the other libs to be provided
5. you should now be able to Build the Plugin: **Build > Prepare Plugin Module <...> For Deployment**


Notes:
---

I basically merged https://github.com/mprzytulski/phpstorm-phpspec and https://github.com/mprzytulski/phpstorm-commons
-> all the work is comming from @mprzytulski and I'm just bugfixing / providing an info of how to build the plugin
54 changes: 54 additions & 0 deletions src/pl/projectspace/idea/plugins/commons/php/ProjectComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package pl.projectspace.idea.plugins.commons.php;

import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.project.Project;
import com.jetbrains.php.PhpIndex;
import org.jetbrains.annotations.NotNull;

/**
* @author Michal Przytulski <[email protected]>
*/
public class ProjectComponent implements com.intellij.openapi.components.ProjectComponent {

protected final Project project;
protected final PhpIndex index;

public ProjectComponent(Project project, PhpIndex index) {
this.project = project;
this.index = index;
}

@Override
public void projectOpened() {
}

@Override
public void projectClosed() {
}

@Override
public void initComponent() {
}

@Override
public void disposeComponent() {
}

@NotNull
@Override
public String getComponentName() {
return null;
}

public PhpIndex getIndex() {
return index;
}

public <T>T getService(@NotNull Class<T> service) {
return (T) ServiceManager.getService(project, service);
}

public Project getProject() {
return project;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pl.projectspace.idea.plugins.commons.php;

public interface StateComponentInterface {

public boolean isEnabled();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package pl.projectspace.idea.plugins.commons.php.action;

import com.intellij.ide.projectView.ProjectView;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;

/**
* @author Michal Przytulski <[email protected]>
*/
public abstract class DirectoryAction extends AnAction {

protected Project project;

@Override
public void actionPerformed(AnActionEvent anActionEvent) {
project = anActionEvent.getProject();

DialogWrapper dialog = (DialogWrapper) getDialog();
dialog.show();

if (dialog.getExitCode() == DialogWrapper.OK_EXIT_CODE) {
onOk(dialog);
}
}

protected String getRelativeDirectory() {
PsiDirectory selected = getSelectedDirectory();

if (selected == null) {

}

return getSelectedDirectory().getVirtualFile().getPath().replace(project.getBasePath(), "");
}

protected PsiDirectory getSelectedDirectory() {
ProjectView view = ProjectView.getInstance(project);
PsiDirectory[] selected = view.getCurrentProjectViewPane().getSelectedDirectories();

if (selected.length == 0) {
Object[] elements = view.getCurrentProjectViewPane().getSelectedElements();
for (Object element : elements) {
if (element instanceof PsiFile) {
return ((PsiFile) element).getContainingDirectory();
}
}
return null;
}

return selected[0];
}

protected abstract DialogWrapper getDialog();

protected abstract String getActionDirectory();

protected abstract void onOk(DialogWrapper dialog);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package pl.projectspace.idea.plugins.commons.php.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.components.BaseComponent;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.Condition;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.psi.PhpFile;
import com.jetbrains.php.lang.psi.PhpPsiUtil;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import pl.projectspace.idea.plugins.commons.php.StateComponentInterface;
import pl.projectspace.idea.plugins.commons.php.utils.annotation.PluginAction;

/**
* @author Michal Przytulski <[email protected]>
*/
public abstract class PhpClassAction extends AnAction {

@Override
public void update(AnActionEvent e) {
final PhpClass phpClass = getPhpClassFromContext(e);

boolean isAvailable = isAvailable(phpClass);

e.getPresentation().setEnabled(
isAvailable
);

e.getPresentation().setVisible(
isAvailable
);

String label = getLabel(phpClass);

if (label != null) {
e.getPresentation().setText(label);
}
}

public void actionPerformed(final AnActionEvent e) {
final PhpClass phpClass = getPhpClassFromContext(e);
perform(phpClass);
}

/**
* Base check if current perform is available - depending on plugin state.
*
* @param phpClass
* @return
*/
protected boolean isAvailable(final PhpClass phpClass) {
PluginAction annotation = this.getClass().getAnnotation(PluginAction.class);

BaseComponent component = phpClass.getProject().getComponent(annotation.value());
if (component instanceof StateComponentInterface) {
return ((StateComponentInterface) component).isEnabled();
}

return false;
}

protected PhpClass getPhpClassFromContext(AnActionEvent e) {
PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
Editor editor = e.getData(PlatformDataKeys.EDITOR);

if (psiFile == null || editor == null) {
return null;
}

int offset = editor.getCaretModel().getOffset();
PsiElement elementAt = psiFile.findElementAt(offset);

return PsiTreeUtil.getParentOfType(elementAt, PhpClass.class);
}

protected String getLabel(PhpClass phpClass) {
return null;
}

/**
* Perform perform
*
* @param phpClass
*/
protected abstract void perform(final PhpClass phpClass);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package pl.projectspace.idea.plugins.commons.php.code.completion;

import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionProvider;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.openapi.components.BaseComponent;
import com.intellij.psi.PsiElement;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;
import pl.projectspace.idea.plugins.commons.php.StateComponentInterface;
import pl.projectspace.idea.plugins.commons.php.utils.annotation.DependsOnPlugin;


public abstract class GenericCompletionProvider extends CompletionProvider<CompletionParameters> {

public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
if (!isEnabled(parameters.getPosition())) {
return;
}

addCompletionsFor(parameters, context, resultSet);
}

protected boolean isEnabled(PsiElement element) {
DependsOnPlugin annotation = this.getClass().getAnnotation(DependsOnPlugin.class);

BaseComponent component = element.getProject().getComponent(annotation.value());
if (component instanceof StateComponentInterface) {
return ((StateComponentInterface) component).isEnabled();
}

return false;
}

protected abstract void addCompletionsFor(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pl.projectspace.idea.plugins.commons.php.code.completion;

import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionProvider;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.openapi.components.BaseComponent;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import org.jetbrains.annotations.NotNull;
import pl.projectspace.idea.plugins.commons.php.StateComponentInterface;
import pl.projectspace.idea.plugins.commons.php.psi.lookup.SimpleTextLookup;
import pl.projectspace.idea.plugins.commons.php.utils.annotation.DependsOnPlugin;
import pl.projectspace.idea.plugins.commons.php.utils.annotation.RequireMethod;

import java.util.List;

/**
* @author Michal Przytulski <[email protected]>
*/
public abstract class GenericMethodArgumentCompletionProvider extends CompletionProvider<CompletionParameters> {

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
MethodReference method = PsiTreeUtil.getParentOfType(parameters.getPosition(), MethodReference.class);
if (method == null || !isEnabled(method)) {
return;
}

RequireMethod methodAnnotation = this.getClass().getAnnotation(RequireMethod.class);
if (methodAnnotation != null && !methodAnnotation.value().equalsIgnoreCase(method.getName())) {
return;
}

for (String item : getCompletions(method)) {
completionResultSet.addElement(new SimpleTextLookup(item));
}
}

protected boolean isEnabled(PsiElement element) {
DependsOnPlugin annotation = this.getClass().getAnnotation(DependsOnPlugin.class);
BaseComponent component = element.getProject().getComponent(annotation.value());

return ((StateComponentInterface) component).isEnabled();
}

protected abstract List<String> getCompletions(MethodReference method);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pl.projectspace.idea.plugins.commons.php.code.completion;

import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionProvider;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.util.ProcessingContext;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

/**
* @author Michal Przytulski <[email protected]>
*/
public class YamlCompletionProvider extends CompletionProvider<CompletionParameters> {

private ArrayList<LookupElement> lookupElements;

YamlCompletionProvider(ArrayList<LookupElement> lookups) {
lookupElements = lookups;
}

public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
resultSet.addAllElements(lookupElements);
}
}
Loading

0 comments on commit 6fceaed

Please sign in to comment.