Migrate IdeLenientImportProvider to lynglib, add detailed time-related documentation, and improve analysis support for unresolved references.
This commit is contained in:
parent
2e7c28f735
commit
74d911e837
@ -25,6 +25,7 @@ import kotlinx.coroutines.runBlocking
|
|||||||
import net.sergeych.lyng.binding.BindingSnapshot
|
import net.sergeych.lyng.binding.BindingSnapshot
|
||||||
import net.sergeych.lyng.miniast.DocLookupUtils
|
import net.sergeych.lyng.miniast.DocLookupUtils
|
||||||
import net.sergeych.lyng.miniast.MiniScript
|
import net.sergeych.lyng.miniast.MiniScript
|
||||||
|
import net.sergeych.lyng.tools.IdeLenientImportProvider
|
||||||
import net.sergeych.lyng.tools.LyngAnalysisRequest
|
import net.sergeych.lyng.tools.LyngAnalysisRequest
|
||||||
import net.sergeych.lyng.tools.LyngAnalysisResult
|
import net.sergeych.lyng.tools.LyngAnalysisResult
|
||||||
import net.sergeych.lyng.tools.LyngLanguageTools
|
import net.sergeych.lyng.tools.LyngLanguageTools
|
||||||
|
|||||||
@ -102,6 +102,7 @@ object BuiltinDocRegistry : BuiltinDocSource {
|
|||||||
// Register built-in lazy seeds
|
// Register built-in lazy seeds
|
||||||
init {
|
init {
|
||||||
registerLazy("lyng.stdlib") { buildStdlibDocs() }
|
registerLazy("lyng.stdlib") { buildStdlibDocs() }
|
||||||
|
registerLazy("lyng.time") { buildTimeDocs() }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -658,7 +659,32 @@ private fun buildStdlibDocs(): List<MiniDecl> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
decls += mod.build()
|
decls += mod.build()
|
||||||
|
|
||||||
return decls
|
return decls
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun buildTimeDocs(): List<MiniDecl> {
|
||||||
|
val mod = ModuleDocsBuilder("lyng.time")
|
||||||
|
mod.classDoc(name = "Instant", doc = "Point in time (epoch-based).", bases = listOf(type("Obj"))) {
|
||||||
|
field(name = "distantFuture", doc = "An instant in the distant future.", type = type("lyng.Instant"), isStatic = true)
|
||||||
|
field(name = "distantPast", doc = "An instant in the distant past.", type = type("lyng.Instant"), isStatic = true)
|
||||||
|
method(name = "now", doc = "Return the current instant.", returns = type("lyng.Instant"), isStatic = true)
|
||||||
|
field(name = "epochSeconds", doc = "Seconds since Unix epoch.", type = type("lyng.Real"))
|
||||||
|
field(name = "epochWholeSeconds", doc = "Full seconds since Unix epoch.", type = type("lyng.Int"))
|
||||||
|
field(name = "nanosecondsOfSecond", doc = "Nanoseconds within the current second.", type = type("lyng.Int"))
|
||||||
|
method(name = "toRFC3339", doc = "Format as RFC3339 string.", returns = type("lyng.String"))
|
||||||
|
method(name = "toDateTime", doc = "Convert to localized DateTime.", params = listOf(ParamDoc("timeZone")), returns = type("lyng.DateTime"))
|
||||||
|
method(name = "truncateToMinute", doc = "Truncate to minute.", returns = type("lyng.Instant"))
|
||||||
|
method(name = "truncateToSecond", doc = "Truncate to second.", returns = type("lyng.Instant"))
|
||||||
|
method(name = "truncateToMillisecond", doc = "Truncate to millisecond.", returns = type("lyng.Instant"))
|
||||||
|
method(name = "truncateToMicrosecond", doc = "Truncate to microsecond.", returns = type("lyng.Instant"))
|
||||||
|
}
|
||||||
|
mod.classDoc(name = "DateTime", doc = "Localized date and time.", bases = listOf(type("Obj"))) {
|
||||||
|
method(name = "toInstant", doc = "Convert back to absolute Instant.", returns = type("lyng.Instant"))
|
||||||
|
}
|
||||||
|
mod.classDoc(name = "Duration", doc = "Time duration.", bases = listOf(type("Obj")))
|
||||||
|
mod.funDoc(name = "delay", doc = "Suspend execution.", params = listOf(ParamDoc("duration")))
|
||||||
|
return mod.build()
|
||||||
|
}
|
||||||
|
|
||||||
// (Registration for external modules is provided by their own libraries)
|
// (Registration for external modules is provided by their own libraries)
|
||||||
|
|||||||
@ -21,9 +21,7 @@ import kotlinx.datetime.*
|
|||||||
import kotlinx.serialization.json.JsonElement
|
import kotlinx.serialization.json.JsonElement
|
||||||
import kotlinx.serialization.json.JsonPrimitive
|
import kotlinx.serialization.json.JsonPrimitive
|
||||||
import net.sergeych.lyng.Scope
|
import net.sergeych.lyng.Scope
|
||||||
import net.sergeych.lyng.miniast.addFnDoc
|
import net.sergeych.lyng.miniast.*
|
||||||
import net.sergeych.lyng.miniast.addPropertyDoc
|
|
||||||
import net.sergeych.lyng.miniast.type
|
|
||||||
import net.sergeych.lynon.LynonDecoder
|
import net.sergeych.lynon.LynonDecoder
|
||||||
import net.sergeych.lynon.LynonEncoder
|
import net.sergeych.lynon.LynonEncoder
|
||||||
import net.sergeych.lynon.LynonSettings
|
import net.sergeych.lynon.LynonSettings
|
||||||
@ -274,14 +272,28 @@ class ObjInstant(val instant: Instant,val truncateMode: LynonSettings.InstantTru
|
|||||||
|
|
||||||
// class members
|
// class members
|
||||||
|
|
||||||
addClassConst("distantFuture", distantFuture)
|
addClassConstDoc(
|
||||||
addClassConst("distantPast", distantPast)
|
name = "distantFuture",
|
||||||
addClassFn("now") {
|
value = distantFuture,
|
||||||
|
doc = "An instant in the distant future.",
|
||||||
|
type = type("lyng.Instant"),
|
||||||
|
moduleName = "lyng.time"
|
||||||
|
)
|
||||||
|
addClassConstDoc(
|
||||||
|
name = "distantPast",
|
||||||
|
value = distantPast,
|
||||||
|
doc = "An instant in the distant past.",
|
||||||
|
type = type("lyng.Instant"),
|
||||||
|
moduleName = "lyng.time"
|
||||||
|
)
|
||||||
|
addClassFnDoc(
|
||||||
|
name = "now",
|
||||||
|
doc = "Return the current instant from the system clock.",
|
||||||
|
returns = type("lyng.Instant"),
|
||||||
|
moduleName = "lyng.time"
|
||||||
|
) {
|
||||||
ObjInstant(Clock.System.now())
|
ObjInstant(Clock.System.now())
|
||||||
}
|
}
|
||||||
// addFn("epochMilliseconds") {
|
|
||||||
// ObjInt(instant.toEpochMilliseconds())
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2025 Sergey S. Chernov real.sergeych@gmail.com
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package net.sergeych.lyng.idea.util
|
package net.sergeych.lyng.tools
|
||||||
|
|
||||||
import net.sergeych.lyng.ModuleScope
|
import net.sergeych.lyng.ModuleScope
|
||||||
import net.sergeych.lyng.Pos
|
import net.sergeych.lyng.Pos
|
||||||
@ -28,7 +28,13 @@ import net.sergeych.lyng.pacman.ImportProvider
|
|||||||
* the compiler can still build MiniAst for Quick Docs / highlighting.
|
* the compiler can still build MiniAst for Quick Docs / highlighting.
|
||||||
*/
|
*/
|
||||||
class IdeLenientImportProvider private constructor(root: Scope) : ImportProvider(root) {
|
class IdeLenientImportProvider private constructor(root: Scope) : ImportProvider(root) {
|
||||||
override suspend fun createModuleScope(pos: Pos, packageName: String): ModuleScope = ModuleScope(this, pos, packageName)
|
override suspend fun createModuleScope(pos: Pos, packageName: String): ModuleScope {
|
||||||
|
return try {
|
||||||
|
Script.defaultImportManager.createModuleScope(pos, packageName)
|
||||||
|
} catch (_: Throwable) {
|
||||||
|
ModuleScope(this, pos, packageName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
/** Create a provider based on the default manager's root scope. */
|
/** Create a provider based on the default manager's root scope. */
|
||||||
@ -39,7 +39,8 @@ data class LyngAnalysisRequest(
|
|||||||
val text: String,
|
val text: String,
|
||||||
val fileName: String = "<snippet>",
|
val fileName: String = "<snippet>",
|
||||||
val importProvider: ImportProvider = Script.defaultImportManager,
|
val importProvider: ImportProvider = Script.defaultImportManager,
|
||||||
val seedScope: Scope? = null
|
val seedScope: Scope? = null,
|
||||||
|
val allowUnresolvedRefs: Boolean = true
|
||||||
)
|
)
|
||||||
|
|
||||||
enum class LyngDiagnosticSeverity { Error, Warning }
|
enum class LyngDiagnosticSeverity { Error, Warning }
|
||||||
@ -95,6 +96,10 @@ data class LyngSemanticSpan(
|
|||||||
object LyngLanguageTools {
|
object LyngLanguageTools {
|
||||||
|
|
||||||
suspend fun analyze(request: LyngAnalysisRequest): LyngAnalysisResult {
|
suspend fun analyze(request: LyngAnalysisRequest): LyngAnalysisResult {
|
||||||
|
// Ensure stdlib/Obj* docs are initialized and stdlib docs are available before anything else
|
||||||
|
StdlibDocsBootstrap.ensure()
|
||||||
|
BuiltinDocRegistry.docsForModule("lyng.stdlib")
|
||||||
|
|
||||||
val source = Source(request.fileName, request.text)
|
val source = Source(request.fileName, request.text)
|
||||||
val miniSink = MiniAstBuilder()
|
val miniSink = MiniAstBuilder()
|
||||||
val resolutionCollector = ResolutionCollector(source.fileName)
|
val resolutionCollector = ResolutionCollector(source.fileName)
|
||||||
@ -107,7 +112,7 @@ object LyngLanguageTools {
|
|||||||
miniSink = miniSink,
|
miniSink = miniSink,
|
||||||
resolutionSink = resolutionCollector,
|
resolutionSink = resolutionCollector,
|
||||||
compileBytecode = false,
|
compileBytecode = false,
|
||||||
allowUnresolvedRefs = true,
|
allowUnresolvedRefs = request.allowUnresolvedRefs,
|
||||||
seedScope = request.seedScope
|
seedScope = request.seedScope
|
||||||
)
|
)
|
||||||
} catch (t: Throwable) {
|
} catch (t: Throwable) {
|
||||||
|
|||||||
@ -0,0 +1,87 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package net.sergeych.lyng.tools
|
||||||
|
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import net.sergeych.lyng.miniast.BuiltinDocRegistry
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
class ReproInstantErrorStrictTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun unknownMemberNoDiagnosticInStrictMode() = runTest {
|
||||||
|
// Clear the registry to simulate fresh start
|
||||||
|
BuiltinDocRegistry.clearModule("lyng.stdlib")
|
||||||
|
BuiltinDocRegistry.clearModule("lyng.time")
|
||||||
|
|
||||||
|
val code = """
|
||||||
|
import lyng.time
|
||||||
|
|
||||||
|
fun fff() {
|
||||||
|
Instant.nowWrong()
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
val provider = IdeLenientImportProvider.create()
|
||||||
|
val result = LyngLanguageTools.analyze(
|
||||||
|
LyngAnalysisRequest(
|
||||||
|
text = code,
|
||||||
|
importProvider = provider,
|
||||||
|
allowUnresolvedRefs = false
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
println("[DEBUG_LOG] Diagnostics: ${result.diagnostics.joinToString { "${it.severity}: ${it.message}" }}")
|
||||||
|
println("[DEBUG_LOG] Resolution Errors: ${result.resolution?.errors?.joinToString { it.message }}")
|
||||||
|
|
||||||
|
val errors = result.diagnostics.filter { it.severity == LyngDiagnosticSeverity.Error }
|
||||||
|
assertTrue(errors.isEmpty(), "Compiler does not report unknown member at analysis stage; diagnostics were: ${errors.joinToString { it.message }}")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun instantNowResolvesWhenStrict() = runTest {
|
||||||
|
// Clear the registry to simulate fresh start
|
||||||
|
BuiltinDocRegistry.clearModule("lyng.stdlib")
|
||||||
|
BuiltinDocRegistry.clearModule("lyng.time")
|
||||||
|
|
||||||
|
val code = """
|
||||||
|
import lyng.time
|
||||||
|
|
||||||
|
fun fff() {
|
||||||
|
Instant.now()
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
val provider = IdeLenientImportProvider.create()
|
||||||
|
val result = LyngLanguageTools.analyze(
|
||||||
|
LyngAnalysisRequest(
|
||||||
|
text = code,
|
||||||
|
importProvider = provider,
|
||||||
|
allowUnresolvedRefs = false
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
println("[DEBUG_LOG] Diagnostics: ${result.diagnostics.joinToString { "${it.severity}: ${it.message}" }}")
|
||||||
|
println("[DEBUG_LOG] Resolution Errors: ${result.resolution?.errors?.joinToString { it.message }}")
|
||||||
|
|
||||||
|
val errors = result.diagnostics.filter { it.severity == LyngDiagnosticSeverity.Error }
|
||||||
|
assertTrue(errors.isEmpty(), "Should not have any errors, but got: ${errors.joinToString { it.message }}")
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package net.sergeych.lyng.tools
|
||||||
|
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import net.sergeych.lyng.miniast.BuiltinDocRegistry
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
class ReproInstantErrorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testInstantResolutionOnFirstPass() = runTest {
|
||||||
|
// Clear the registry to simulate fresh start
|
||||||
|
BuiltinDocRegistry.clearModule("lyng.stdlib")
|
||||||
|
BuiltinDocRegistry.clearModule("lyng.time")
|
||||||
|
|
||||||
|
val code = """
|
||||||
|
import lyng.time
|
||||||
|
|
||||||
|
fun fff() {
|
||||||
|
Instant.now()
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
|
// Analyze without any prior "touches"
|
||||||
|
val result = LyngLanguageTools.analyze(code)
|
||||||
|
|
||||||
|
// Check if there are any errors related to Instant
|
||||||
|
val instantErrors = result.diagnostics.filter { it.message.contains("Instant") }
|
||||||
|
|
||||||
|
assertTrue(instantErrors.isEmpty(), "Should not have Instant-related errors on first pass, but got: ${instantErrors.joinToString { it.message }}")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user