Skip to content

Commit

Permalink
#920,#930 upgrade grammarkit plugin to fix build with version 2024.1 (#…
Browse files Browse the repository at this point in the history
…929)

* #920 upgrade grammarkit plugin to fix build with version 2024.1

* #930: Instanciate JDK only when necessary and stop using SdkMock

---------

Co-authored-by: Etienne Brateau <etienne.brateau@gmail.com>
  • Loading branch information
etienne02 and etiennebrateau committed Apr 7, 2024
1 parent 06c3a1a commit 8d8789f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
13 changes: 4 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ intellij {
buildPlugin { enabled = true }
downloadRobotServerPlugin { enabled = true }
jarSearchableOptions { enabled = true }
patchPluginXml { enabled = true }
patchPluginXml { enabled = false }
listProductsReleases { enabled = false }
prepareSandbox { enabled = true }
prepareTestingSandbox { enabled = true }
prepareUiTestingSandbox { enabled = true }
Expand Down Expand Up @@ -237,21 +238,15 @@ val generateSyntaxLexer = tasks.register<GenerateLexerTask>("generateSyntaxLexer
sourceFile.set(file("src/main/jflex/io/github/intellij/dlanguage/lexer/DLanguageLexer.flex"))

// target directory for lexer
targetDir.set("gen/io/github/intellij/dlanguage/")

// target classname, target file will be targetDir/targetClass.java
targetClass.set("DlangLexer")
targetOutputDir.set(file("gen/io/github/intellij/dlanguage/"))
}

val generateDocSyntaxLexer = tasks.register<GenerateLexerTask>("generateDocSyntaxLexer") {
// source flex file
sourceFile.set(file("src/main/kotlin/io/github/intellij/dlanguage/features/documentation/DDocLexer.flex"))

// target directory for lexer
targetDir.set("gen/io/github/intellij/dlanguage/features/documentation")

// target classname, target file will be targetDir/targetClass.java
targetClass.set("_DDocLexer")
targetOutputDir.set(file("gen/io/github/intellij/dlanguage/features/documentation/"))
}

val copyGenScript = tasks.register<Copy>("copyGenScript") {
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sentry = "7+"

# plugins
gradleIntelliJPlugin = "1.10.1" # cannot update to latest as it breaks verifyPlugin
grammarkit = "2022.3.1"
grammarkit = "2022.3.2.2"
kotlin = "1.8.22"

[plugins]
Expand Down
7 changes: 2 additions & 5 deletions sdlang/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,12 @@ val generateSyntaxLexer = tasks.register<GenerateLexerTask>("generateSyntaxLexer
sourceFile.set(file("src/main/kotlin/io/github/intellij/dlanguage/sdlang/lexer/SDLangLexer.flex"))

// target directory for lexer
targetDir.set("gen/io/github/intellij/dlanguage/sdlang/lexer")

// target classname, target file will be targetDir/targetClass.java
targetClass.set("_SDLangLexer")
targetOutputDir.set(file("gen/io/github/intellij/dlanguage/sdlang/lexer"))
}

val generateSyntaxParser = tasks.register<GenerateParserTask>("generateSyntaxParser") {
sourceFile.set(file("src/main/kotlin/io/github/intellij/dlanguage/sdlang/parser/SDLangParser.bnf"))
targetRoot.set("gen")
targetRootOutputDir.set(file("gen"))
pathToParser.set("io/github/intellij/dlanguage/sdlang/parser/SDLangParser.java")
pathToPsiRoot.set("io/github/intellij/dlanguage/sdlang/psi")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
package io.github.intellij.dlanguage.codeinsight.dcd;

import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.projectRoots.*;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.VfsTestUtil;
import com.intellij.util.PathUtil;
import io.github.intellij.dlanguage.DlangSdkType;
import io.github.intellij.dlanguage.LightDlangTestCase;
import org.junit.Test;

import java.io.File;

public class DCDCompletionServerTest extends LightDlangTestCase {

// this method will be called prior to the test being run and MockSdk is read only
@Override
public Sdk getProjectJDK() {
// Get a tmp folder to be the root of our "fake sdk"
var tmp = new File(FileUtil.getTempDirectory());
sdkRoot = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tmp);
var phobos = VfsTestUtil.createDir(sdkRoot, "phobos");
var druntime = VfsTestUtil.createDir(sdkRoot, "druntime");

// Configure a sdk
var dlangSdk = DlangSdkType.getInstance();
var sdk = ProjectJdkTable.getInstance().createSdk("dmd", dlangSdk);
var sdkModificator = sdk.getSdkModificator();
sdkModificator.setVersionString("2");
sdkModificator.addRoot(phobos.getUrl(), OrderRootType.SOURCES);
sdkModificator.addRoot(druntime.getUrl(), OrderRootType.SOURCES);
// Ensure it is saved to be detected by the later code
Application application = ApplicationManager.getApplication();
Runnable runnable = sdkModificator::commitChanges;
if (application.isDispatchThread()) {
application.runWriteAction(runnable);
} else {
application.invokeAndWait(() -> application.runWriteAction(runnable));
}
return sdk;
}

private DCDCompletionServer dcd;
private VirtualFile sdkRoot;

@Override
public void setUp() throws Exception {
Expand All @@ -21,8 +60,8 @@ public void testBuildDcdCommand() {

final String expected = String.format("dcd-server --ignoreConfig --logLevel all -I %s -I %s -I %s",
PathUtil.toSystemDependentName("/src"),
PathUtil.toSystemDependentName("/phobos"),
PathUtil.toSystemDependentName("/druntime"));
PathUtil.toSystemDependentName(sdkRoot.getPath() + "/phobos"),
PathUtil.toSystemDependentName(sdkRoot.getPath() + "/druntime"));

assertEquals(expected, cmd.getCommandLineString());
}
Expand Down
19 changes: 0 additions & 19 deletions src/test/kotlin/io/github/intellij/dlanguage/LightDlangTestCase.kt
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
package io.github.intellij.dlanguage

import com.intellij.mock.MockPsiManager
import com.intellij.openapi.projectRoots.Sdk
import com.intellij.openapi.projectRoots.impl.MockSdk
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.OrderRootType
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.FileViewProvider
import com.intellij.psi.SingleRootFileViewProvider
import com.intellij.testFramework.LightPlatform4TestCase
import com.intellij.testFramework.LightVirtualFile
import io.github.intellij.dlanguage.psi.DlangFile
import com.intellij.testFramework.VfsTestUtil
import com.intellij.util.containers.MultiMap

/**
* Provides a base for unit testing that requires in-memory D source files
*/
abstract class LightDlangTestCase : LightPlatform4TestCase() {

companion object {
class MockDir(val dirName: String) : LightVirtualFile(dirName) {
override fun isDirectory(): Boolean = true
}
}

// this method will be called prior to the test being run and MockSdk is read only
override fun getProjectJDK(): Sdk {
val roots = MultiMap<OrderRootType, VirtualFile>(2)
roots.putValue(OrderRootType.SOURCES, MockDir("phobos"))
roots.putValue(OrderRootType.SOURCES, MockDir("druntime"))

return MockSdk("dmd", "", "2", roots) { DlangSdkType.getInstance() }
}

fun addFileToModuleSource(filename: String, content: String? = null): VirtualFile {
val sourcesRoot = ModuleRootManager.getInstance(module).sourceRoots[0]
return VfsTestUtil.createFile(sourcesRoot, filename, content)
Expand Down

0 comments on commit 8d8789f

Please sign in to comment.