Skip to content

Commit

Permalink
Add support for PHPStorm and NativeTypesLanguage.Php.
Browse files Browse the repository at this point in the history
  • Loading branch information
Layoric committed Oct 18, 2023
1 parent 968bb6b commit 29de49f
Show file tree
Hide file tree
Showing 17 changed files with 701 additions and 21 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# ServiceStackIDEA Changelog

## [Unreleased]
- Add support for PHPStorm and Add ServiceStack Reference
- Resolve some issues with IntelliJ context menus
## [1.3.4]
- Fix issue for Kotlin setups using `build.gradle.kts` files.
## [1.3.3]
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ tasks {
from("src/main/resources/META-INF") {
include("intellij.xml")
include("pycharm.xml")
include("phpstorm.xml")
}
}
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

pluginGroup = ServiceStackIDEA
pluginName = ServiceStack
pluginVersion = 1.3.4
pluginVersion = 1.4.0

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
Expand All @@ -20,7 +20,7 @@ platformDownloadSources = true

# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
platformPlugins = maven, java, PythonCore:211.6693.119
platformPlugins = maven, java, PythonCore:211.6693.119,com.jetbrains.php:211.6693.120

# Opt-out flag for bundling Kotlin standard library.
# See https://plugins.jetbrains.com/docs/intellij/kotlin.html#kotlin-standard-library for details.
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/net/servicestack/idea/AddPythonAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.jetbrains.python.PythonFileType;
import com.jetbrains.python.facet.PythonFacet;
import com.jetbrains.python.facet.PythonFacetSettings;
import net.servicestack.idea.common.INativeTypesHandler;
import org.jetbrains.annotations.NotNull;

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/servicestack/idea/AddPythonRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public boolean verify(JComponent input) {
text = ((JTextField) input).getText();
}

return text != null && text.length() > 0;
return text != null && !text.isEmpty();
}

@Override
Expand All @@ -78,7 +78,7 @@ public boolean verify(JComponent input) {
text = ((JTextField) input).getText();
}

return text != null && text.length() > 0;
return text != null && !text.isEmpty();
}

@Override
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/net/servicestack/idea/GradleBuildFileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,37 +130,46 @@ public static Boolean isUsingKotlin(AnActionEvent event){
public static File getGradleBuildFile(AnActionEvent event) {
VirtualFile vFile = event.getData(CommonDataKeys.VIRTUAL_FILE);
Project project = event.getData(CommonDataKeys.PROJECT);
assert project != null;

if (project == null || vFile == null) {
return null;
}

String projectBase = project.getBasePath();
assert projectBase != null;
if (projectBase == null) {
return null;
}

File projectBaseFile = new File(projectBase);
projectBase = projectBaseFile.getAbsolutePath();
assert vFile != null;
String basePath = vFile.isDirectory() ? vFile.getPath() : vFile.getParent().getPath();
File file = new File(basePath);
File gradleFile = null;

int count = 0;
int maxDepth = 8;
while(true) {

while (true) {
File[] matchingFiles = file.listFiles((dir, name) -> name.startsWith("build.gradle"));
boolean foundFile = matchingFiles != null && matchingFiles.length != 0;

if(foundFile) {
if (foundFile) {
gradleFile = matchingFiles[0];
break;
}

// project base even on Windows value is "c:/x/" using the wrong file separator.
if(file.getAbsolutePath().equals(projectBase) || count >= maxDepth) {
if (file.getAbsolutePath().equals(projectBase) || count >= maxDepth) {
break;
}

count++;
file = file.getParentFile();
}
return gradleFile;

}


public static File getDartPubspec(Module module) {
String projDir = module.getProject().getBasePath();
if (projDir == null) {
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/net/servicestack/idea/IDEAPomFileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ private void PomAppendDependency(final Module module, final File pomFile, String
newDepNode.appendChild(groupNode);
newDepNode.appendChild(artifactNode);
newDepNode.appendChild(versionNode);
assert dependenciesNode != null;
if(dependenciesNode == null) {
return;
}
dependenciesNode.appendChild(newDepNode);

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Expand All @@ -101,10 +103,14 @@ private void PomAppendDependency(final Module module, final File pomFile, String

final Project project = module.getProject();
final VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(pomFile);
assert virtualFile != null;
if(virtualFile == null) {
return;
}
final com.intellij.openapi.editor.Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
final FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
assert document != null;
if(document == null) {
return;
}
fileDocumentManager.saveDocument(document); //when file is edited and editor is closed, it is needed to save the text
PsiDocumentManager.getInstance(project).commitDocument(document);
PsiDocumentManager.getInstance(module.getProject()).commitAllDocuments();
Expand Down Expand Up @@ -148,7 +154,9 @@ public static boolean pomHasMavenDependency(File pomFile, String groupId, String
}

Node dependencies = getMavenDependenciesNode(doc);
assert dependencies != null;
if(dependencies == null) {
return false;
}
NodeList depElements = dependencies.getChildNodes();
for (int i = 0; i < depElements.getLength(); i++) {
Node dependencyElement = depElements.item(i);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/servicestack/idea/NativeTypeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import net.servicestack.idea.common.MjsNativeTypesHandler;
import net.servicestack.idea.common.TypeScriptConcreteNativeTypesHandler;
import net.servicestack.idea.common.TypeScriptNativeTypesHandler;
import net.servicestack.idea.php.PhpNativeTypesHandler;

public class NativeTypeUtils {

public static INativeTypesHandler getNativeTypesHandler(String fileName) {
INativeTypesHandler result = null;
if (fileName.endsWith(".kt")) result = new KotlinNativeTypesHandler();
if (fileName.endsWith(".java")) result = new JavaNativeTypesHandler();
if (fileName.endsWith(".php")) result = new PhpNativeTypesHandler();
if (fileName.endsWith("dtos.dart")) result = new DartNativeTypesHandler();
if (fileName.endsWith("dtos.ts")) result = new TypeScriptConcreteNativeTypesHandler();
if (fileName.endsWith("dtos.d.ts")) result = new TypeScriptNativeTypesHandler();
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/net/servicestack/idea/common/IDEAUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -63,7 +65,7 @@ public static boolean writeDtoFile(List<String> codeLines, String path, StringBu
BufferedWriter writer = null;
boolean result = true;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), StandardCharsets.UTF_8));
writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(Paths.get(path)), StandardCharsets.UTF_8));
for (String item : codeLines) {
writer.write(item);
writer.newLine();
Expand All @@ -73,8 +75,8 @@ public static boolean writeDtoFile(List<String> codeLines, String path, StringBu
errorMessage.append("Error writing DTOs to file - ").append(ex.getMessage());
} finally {
try {
assert writer != null;
writer.close();
if(writer != null)
writer.close();
} catch (Exception ignored) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public enum NativeTypesLanguage {
Java,
Kotlin,
Mjs,
Php,
Python,
TypeScript,
TypeScriptConcrete,
Expand Down
103 changes: 103 additions & 0 deletions src/main/java/net/servicestack/idea/php/AddPhpAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package net.servicestack.idea.php;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiElement;
import com.jetbrains.php.lang.PhpFileType;
import org.jetbrains.annotations.NotNull;

public class AddPhpAction extends AnAction {
@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
Module module = getModule(anActionEvent);
AddPhpRef dialog = new AddPhpRef(module); // Create your AddPhpRef dialog similar to your existing AddPythonRef
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setSize(dialog.getPreferredSize());
dialog.setResizable(true);
dialog.setTitle("Add PHP ServiceStack Reference");
PsiElement element = LangDataKeys.PSI_ELEMENT.getData(anActionEvent.getDataContext());
if (element instanceof PsiDirectory) {
PsiDirectory selectedDir = (PsiDirectory)element;
dialog.setSelectedDirectory(selectedDir.getVirtualFile().getPath());
dialog.setFileName("dtos");
}
showDialog(dialog);
}

private void showDialog(AddPhpRef dialog) {
dialog.setVisible(true);
}

@Override
public void update(@NotNull AnActionEvent e) {
Module module = getModule(e);
if (module == null) {
e.getPresentation().setEnabled(false);
}

if (!isPhpModule(module)) { // Checking if this is a PHP project
e.getPresentation().setVisible(false);
}

super.update(e);
}

public boolean isPhpModule(Module module) {
if (module == null) {
return false;
}

// Retrieve the module's root manager
ModuleRootManager moduleRootManager = ModuleRootManager.getInstance(module);

// Retrieve the module's content roots
VirtualFile[] roots = moduleRootManager.getContentRoots();

// Iterate over each root file
for (VirtualFile rootFile : roots) {
VirtualFile[] children = rootFile.getChildren();

// Iterate over each child file
for (VirtualFile childFile : children) {
// Check if a child file is 'composer.json' or 'index.php'
String fileName = childFile.getName();
if ("composer.json".equals(fileName) || "index.php".equals(fileName)) {
return true;
}
}
}

// If neither 'composer.json' nor 'index.php' are found, return false
return false;
}


static Module getModule(Project project) {
if (project == null)
return null;
Module[] modules = ModuleManager.getInstance(project).getModules();
if (modules.length > 0) {
return modules[0];
}
return null;
}

static Module getModule(AnActionEvent e) {
Module module = e.getData(LangDataKeys.MODULE);
if (module == null) {
Project project = e.getData(LangDataKeys.PROJECT);
return getModule(project);
} else {
return module;
}
}
}

Loading

0 comments on commit 29de49f

Please sign in to comment.