Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: check ability to detect abstractions and patterns in code INTELLIJ-29 #14

Merged
merged 34 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e715487
chore: poc of basic abstractions
kmruiz Jul 3, 2024
4446cb0
chore: check constructor injection
kmruiz Jul 3, 2024
35ebbc4
chore: support for more abstractions
kmruiz Jul 3, 2024
e034bd3
chore: fix test
kmruiz Jul 3, 2024
431f85b
chore: support custom query dsl abstractions
kmruiz Jul 4, 2024
4f2c6a3
chore: support for mms patterns
kmruiz Jul 4, 2024
3a70ce8
chore: add more basic cases
kmruiz Jul 4, 2024
ae9927c
Merge branch 'main' into chore/intellij-29
kmruiz Jul 4, 2024
b356340
chore: merge with main
kmruiz Jul 4, 2024
b228c7e
chore: run test when the driver is indexed
kmruiz Jul 4, 2024
29c9dfc
chore: we need the driver for testing
kmruiz Jul 4, 2024
b23e237
chore: clean up, we don't need the loop
kmruiz Jul 4, 2024
d23ba1f
chore: add example from the basic project
kmruiz Jul 4, 2024
ba7755b
chore: fix compilation error
kmruiz Jul 4, 2024
41ae81e
chore: fix name of the file
kmruiz Jul 4, 2024
82cce34
chore: add clarifying comments, this is a bit complex
kmruiz Jul 5, 2024
d6fd3d8
chore: simplify tests
kmruiz Jul 5, 2024
3dddb52
chore: support for constants
kmruiz Jul 5, 2024
d38bc5b
chore: show namespace of query
kmruiz Jul 5, 2024
babdee4
chore: refactored so we don't need to check abstractions
kmruiz Jul 8, 2024
2507f6a
chore: add more tests and cleanup
kmruiz Jul 8, 2024
cc99300
chore: remove unused code
kmruiz Jul 8, 2024
bc689c5
chore: make sure nodes support valid components
kmruiz Jul 8, 2024
5cbccbf
chore: add inlay tests
kmruiz Jul 8, 2024
1064a3a
chore: fix reference
kmruiz Jul 8, 2024
402188a
chore: fix test that does not depend on application
kmruiz Jul 9, 2024
3665f20
chore: remove driver binary from project and use gradle dependency fo…
kmruiz Jul 9, 2024
ae05842
Merge branch 'main' into chore/intellij-29
kmruiz Jul 9, 2024
af4594e
chore: add new func to changelog
kmruiz Jul 9, 2024
65820a9
chore: rename to a likely better name
kmruiz Jul 9, 2024
4539fbc
chore: add anna's suggestions and comments
kmruiz Jul 10, 2024
198e7a6
chore: run only coverage if tests were successful
kmruiz Jul 10, 2024
5412bb5
chore: fix test flakiness
kmruiz Jul 10, 2024
4d33199
chore: fix references to new files
kmruiz Jul 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/quality-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ jobs:
sudo apt install -y docker docker-compose
- name: Run Test Suite
run: |
./gradlew --stacktrace --console=plain "clean" "cleanTest" "unitTest" "jacocoTestReport" -x "packages:jetbrains-plugin:uiTest"
./gradlew --stacktrace --console=plain "clean" "cleanTest" "unitTest" -x "packages:jetbrains-plugin:uiTest"
- name: Generate Coverage Report
run: |
./gradlew "jacocoTestReport" $(./gradlew "jacocoTestReport" --dry-run | awk '/^:/ { print "-x" $1 }' | sed '$ d')
- name: Publish Test Report
uses: mikepenz/action-junit-report@v4
if: success() || failure() # always run even if the previous step fails
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ packages/jetbrains-plugin/src/main/resources/build.properties
/**/video
/**/.DS_Store

FAKE_BROWSER_OUTPUT
FAKE_BROWSER_OUTPUT
/.kotlin
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ MongoDB plugin for IntelliJ IDEA.
## [Unreleased]

### Added
* [INTELLIJ-29](https://jira.mongodb.org/browse/INTELLIJ-29): Shows an inlay hint near a Java query that shows in which collection the query is
going to be run in case it could be inferred.
* [INTELLIJ-17](https://jira.mongodb.org/browse/INTELLIJ-17): Added a toolbar that allows to attach a MongoDB data source to the current editor.
This data source is used for autocompletion and type checking.
* [INTELLIJ-14](https://jira.mongodb.org/browse/INTELLIJ-14): Send telemetry when a connection to a MongoDB Cluster fails.
Expand Down
2 changes: 2 additions & 0 deletions packages/jetbrains-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
implementation(project(":packages:mongodb-access-adapter:datagrip-access-adapter"))
implementation(project(":packages:mongodb-autocomplete-engine"))
implementation(project(":packages:mongodb-dialects"))
implementation(project(":packages:mongodb-dialects:java-driver"))
implementation(project(":packages:mongodb-linting-engine"))
implementation(project(":packages:mongodb-mql-model"))

Expand All @@ -40,6 +41,7 @@ dependencies {
testCompileOnly(libs.testing.intellij.ideImpl)
testCompileOnly(libs.testing.intellij.coreUi)

testImplementation(libs.mongodb.driver)
testImplementation(libs.testing.jsoup)
testImplementation(libs.testing.video.recorder)
testImplementation(libs.testing.remoteRobot)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ class EditorToolbarDecorator(

private fun isEditingJavaFileWithMongoDbRelatedCode(): Boolean {
val project = editor.project ?: return false
val psiFile = PsiManager.getInstance(project).findFile(editor.virtualFile) ?: return false
val psiFileResult = runCatching { PsiManager.getInstance(project).findFile(editor.virtualFile) }

if (psiFileResult.isFailure) {
return false
}

val psiFile = psiFileResult.getOrThrow()!!

if (psiFile.language != JavaLanguage.INSTANCE) {
return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.mongodb.jbplugin.inlays

import com.intellij.codeInsight.hints.declarative.*
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiDocumentManager
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.util.parentOfType
import com.mongodb.jbplugin.dialects.javadriver.glossary.NamespaceExtractor
import com.mongodb.jbplugin.dialects.javadriver.glossary.findContainingClass
import com.mongodb.jbplugin.dialects.javadriver.glossary.isMongoDbCollectionClass

/**
* This inlay shows for the current query in which namespace is going to run, if possible,
* according to the extraction rules of NamespaceExtractor.
*
* @see NamespaceExtractor
*/
class JavaDriverQueryNamespaceInlay : InlayHintsProvider {
override fun createCollector(
file: PsiFile,
editor: Editor,
): InlayHintsCollector = QueryNamespaceInlayHintsCollector()

class QueryNamespaceInlayHintsCollector : SharedBypassCollector {
override fun collectFromElement(
element: PsiElement,
sink: InlayTreeSink,
) {
val asMethodCall = element as? PsiMethodCallExpression

val callsAcollection =
asMethodCall
?.methodExpression
?.resolve()
?.findContainingClass()
?.isMongoDbCollectionClass(
element.project,
) == true

val callsSuperClass =
asMethodCall?.findContainingClass() != null &&
asMethodCall.findContainingClass().superClass != null &&
(asMethodCall.methodExpression.resolve() as? PsiMethod)?.containingClass ==
asMethodCall.findContainingClass().superClass

if (callsAcollection || callsSuperClass) {
val namespace =
runCatching {
NamespaceExtractor.extractNamespace(element)
?: NamespaceExtractor.extractNamespace(element.parentOfType<PsiMethod>()!!)
}.getOrNull()

namespace ?: return

val documentManager = PsiDocumentManager.getInstance(element.project)
val document = documentManager.getDocument(element.containingFile)!!
val lineOfElement = document.getLineNumber(element.textOffset)

sink.addPresentation(
EndOfLinePosition(lineOfElement),
emptyList(),
"Inferred MongoDB namespace for this query.",
true,
) {
text(namespace.toString())
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
bundle="messages.TelemetryBundle"
key="notification.group.name"
/>
<codeInsight.declarativeInlayProvider
group="com.mongodb.jbplugin"
implementationClass="com.mongodb.jbplugin.inlays.JavaDriverQueryNamespaceInlay"
isEnabledByDefault="true"
language="JAVA"
nameKey="inlay.namespace.name"
bundle="messages.InspectionsAndInlaysBundle"
providerId="com.mongodb.jbplugin#JAVA" />
</extensions>
<applicationListeners>
</applicationListeners>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
notification.group.inspection.results=MongoDB Inspection Results
inlay.namespace.name=MongoDB Namespace Inlay
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class JavaDriverToolbarVisibilityUiTest {
@Test
@RequiresProject("basic-java-project-with-mongodb")
fun `shows the toolbar in a java file with references to the driver`(remoteRobot: RemoteRobot) {
remoteRobot.ideaFrame().openFile("/src/main/java/alt/mongodb/javadriver/JavaDriverRepositoryExample.java")
remoteRobot.ideaFrame().openFile("/src/main/java/alt/mongodb/javadriver/JavaDriverRepository.java")
val toolbar = remoteRobot.findJavaEditorToolbar()
assertTrue(toolbar.isShowing)
}
Expand All @@ -51,7 +51,7 @@ class JavaDriverToolbarVisibilityUiTest {
remoteRobot: RemoteRobot,
url: MongoDbServerUrl,
) {
remoteRobot.ideaFrame().openFile("/src/main/java/alt/mongodb/javadriver/JavaDriverRepositoryExample.java")
remoteRobot.ideaFrame().openFile("/src/main/java/alt/mongodb/javadriver/JavaDriverRepository.java")

val toolbar = remoteRobot.findJavaEditorToolbar()
assertTrue(toolbar.dataSources.listValues().contains(javaClass.simpleName))
Expand All @@ -63,7 +63,7 @@ class JavaDriverToolbarVisibilityUiTest {
remoteRobot: RemoteRobot,
url: MongoDbServerUrl,
) {
remoteRobot.ideaFrame().openFile("/src/main/java/alt/mongodb/javadriver/JavaDriverRepositoryExample.java")
remoteRobot.ideaFrame().openFile("/src/main/java/alt/mongodb/javadriver/JavaDriverRepository.java")

val toolbar = remoteRobot.findJavaEditorToolbar()
assertTrue(toolbar.dataSources.listValues().contains(javaClass.simpleName))
Expand Down
Loading
Loading