-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lsp): report diagnostics and keep up with changes
- Loading branch information
Showing
22 changed files
with
296 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package tools.samt.common | ||
|
||
import java.io.File | ||
|
||
fun List<File>.readSamtSource(controller: DiagnosticController): List<SourceFile> { | ||
return buildList { | ||
for (file in this@readSamtSource) { | ||
if (!file.exists()) { | ||
controller.reportGlobalError("File '${file.canonicalPath}' does not exist") | ||
continue | ||
} | ||
|
||
if (!file.canRead()) { | ||
controller.reportGlobalError("File '${file.canonicalPath}' cannot be read, bad file permissions?") | ||
continue | ||
} | ||
|
||
if (file.extension != "samt") { | ||
controller.reportGlobalError("File '${file.canonicalPath}' must end in .samt") | ||
continue | ||
} | ||
|
||
add(SourceFile(file.canonicalPath, content = file.readText())) | ||
} | ||
} | ||
} | ||
|
||
fun collectSamtFiles(directory: String): List<File> { | ||
val dir = File(directory) | ||
return dir.walkTopDown().filter { it.isFile && it.extension == "samt" }.toList() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tools.samt.ls | ||
|
||
import tools.samt.common.DiagnosticContext | ||
import tools.samt.common.DiagnosticException | ||
import tools.samt.common.SourceFile | ||
import tools.samt.lexer.Lexer | ||
import tools.samt.lexer.Token | ||
import tools.samt.parser.FileNode | ||
import tools.samt.parser.Parser | ||
|
||
class FileInfo( | ||
val diagnosticContext: DiagnosticContext, | ||
val sourceFile: SourceFile, | ||
val tokens: List<Token>, | ||
val fileNode: FileNode? = null | ||
) | ||
|
||
fun parseFile(sourceFile: SourceFile): FileInfo { | ||
val diagnosticContext = DiagnosticContext(sourceFile) | ||
|
||
val tokens = Lexer.scan(sourceFile.content.reader(), diagnosticContext).toList() | ||
|
||
if (diagnosticContext.hasErrors()) { | ||
return FileInfo(diagnosticContext, sourceFile, tokens) | ||
} | ||
|
||
val fileNode = try { | ||
Parser.parse(sourceFile, tokens.asSequence(), diagnosticContext) | ||
} catch (e: DiagnosticException) { | ||
// error message is added to the diagnostic console, so it can be ignored here | ||
return FileInfo(diagnosticContext, sourceFile, tokens) | ||
} | ||
|
||
return FileInfo(diagnosticContext, sourceFile, tokens, fileNode) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package tools.samt.ls | ||
|
||
import org.eclipse.lsp4j.* | ||
import tools.samt.common.DiagnosticMessage | ||
import tools.samt.common.DiagnosticSeverity | ||
import tools.samt.common.Location as SamtLocation | ||
|
||
fun DiagnosticMessage.toDiagnostic(): Diagnostic? { | ||
val diagnostic = Diagnostic() | ||
val primaryHighlight = this.highlights.firstOrNull() ?: return null | ||
// TODO consider highlightBeginningOnly | ||
diagnostic.range = primaryHighlight.location.toRange() | ||
diagnostic.severity = when (severity) { | ||
DiagnosticSeverity.Error -> org.eclipse.lsp4j.DiagnosticSeverity.Error | ||
DiagnosticSeverity.Warning -> org.eclipse.lsp4j.DiagnosticSeverity.Warning | ||
DiagnosticSeverity.Info -> org.eclipse.lsp4j.DiagnosticSeverity.Information | ||
} | ||
diagnostic.source = "samt" | ||
diagnostic.message = message | ||
diagnostic.relatedInformation = highlights.filter { it.message != null }.map { | ||
DiagnosticRelatedInformation( | ||
Location("file://${it.location.source.absolutePath}", it.location.toRange()), | ||
it.message | ||
) | ||
} | ||
return diagnostic | ||
} | ||
|
||
fun SamtLocation.toRange(): Range { | ||
return Range( | ||
Position(start.row, start.col), | ||
Position(start.row, end.col) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.