Skip to content

Commit

Permalink
Add --display-style json2 to allow for more complex outputs being sen…
Browse files Browse the repository at this point in the history
…t to extensions (#445)
  • Loading branch information
Kampfkarren authored Oct 15, 2022
1 parent 890ae2b commit 716e13b
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased](https://github.com/Kampfkarren/selene/compare/0.22.0...HEAD)
### Added
- Added `--display-style=json2`, which gives the same outputs as `--display-style=json`, but with an extra `type` field so that it can support more than diagnostics. Extensions should move over to `--display-style=json2` as more becomes available for it, but take care to check for `type`. Currently the only possible value is "Diagnostic".

## [0.22.0](https://github.com/Kampfkarren/selene/releases/tag/0.22.0) - 2022-10-15
### Added
Expand Down
2 changes: 1 addition & 1 deletion docs/src/cli/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ FLAGS:
OPTIONS:
--color <color> [default: auto] [possible values: Always, Auto, Never]
--config <config> A toml file to configure the behavior of selene [default: selene.toml]
--display-style <display-style> Sets the display method [possible values: Json, Rich, Quiet]
--display-style <display-style> Sets the display method [possible values: Json, Json2, Rich, Quiet]
--num-threads <num-threads> Number of threads to run on, default to the numbers of logical cores on your
system [default: your system's cores]
--pattern <pattern> A glob to match files with to check
Expand Down
24 changes: 15 additions & 9 deletions selene-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as selene from "./selene"
import * as timers from "timers"
import * as util from "./util"
import * as vscode from "vscode"
import { Diagnostic, Severity, Label } from "./diagnostic"
import { Diagnostic, Severity, Label } from "./structures/diagnostic"
import { Output } from "./structures/output"

let trySelene: Promise<boolean>

Expand Down Expand Up @@ -102,7 +103,7 @@ export async function activate(

const output = await selene.seleneCommand(
context.globalStorageUri,
"--display-style=json --no-summary -",
"--display-style=json2 --no-summary -",
selene.Expectation.Stderr,
vscode.workspace.getWorkspaceFolder(document.uri),
document.getText(),
Expand All @@ -118,13 +119,18 @@ export async function activate(
const byteOffsets = new Set<number>()

for (const line of output.split("\n")) {
const data: Diagnostic = JSON.parse(line)
dataToAdd.push(data)
byteOffsets.add(data.primary_label.span.start)
byteOffsets.add(data.primary_label.span.end)
for (const label of data.secondary_labels) {
byteOffsets.add(label.span.start)
byteOffsets.add(label.span.end)
const output: Output = JSON.parse(line)

switch (output.type) {
case "diagnostic":
dataToAdd.push(output)
byteOffsets.add(output.primary_label.span.start)
byteOffsets.add(output.primary_label.span.end)
for (const label of output.secondary_labels) {
byteOffsets.add(label.span.start)
byteOffsets.add(label.span.end)
}
break
}
}

Expand Down
2 changes: 1 addition & 1 deletion selene-vscode/src/roblox.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from "vscode"
import { Diagnostic } from "./diagnostic"
import { Diagnostic } from "./structures/diagnostic"
import { TextDecoder, TextEncoder } from "util"

const SETUP_CONFIGURATION = "Setup Configuration"
Expand Down
File renamed without changes.
5 changes: 5 additions & 0 deletions selene-vscode/src/structures/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Diagnostic } from "./diagnostic"

export type Output = {
type: "diagnostic"
} & Diagnostic
14 changes: 10 additions & 4 deletions selene/src/json_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ use codespan_reporting::diagnostic::{
use serde::Serialize;

#[derive(Serialize)]
struct JsonDiagnostic {
#[serde(tag = "type")]
pub enum JsonOutput {
Diagnostic(JsonDiagnostic),
}

#[derive(Serialize)]
pub struct JsonDiagnostic {
severity: Severity,
code: Option<String>,
message: String,
Expand Down Expand Up @@ -55,8 +61,8 @@ fn label_to_serializable(
pub fn diagnostic_to_json(
diagnostic: &CodespanDiagnostic<codespan::FileId>,
files: &codespan::Files<&str>,
) -> serde_json::Result<String> {
serde_json::to_string(&JsonDiagnostic {
) -> JsonDiagnostic {
JsonDiagnostic {
code: diagnostic.code.to_owned(),
message: diagnostic.message.to_owned(),
severity: diagnostic.severity.to_owned(),
Expand All @@ -71,5 +77,5 @@ pub fn diagnostic_to_json(
.filter(|label| label.style == LabelStyle::Secondary)
.map(|label| label_to_serializable(label, files))
.collect(),
})
}
}
36 changes: 26 additions & 10 deletions selene/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,32 @@ fn emit_codespan(
..Default::default()
};

if let Some(opts::DisplayStyle::Json) = opts.display_style {
writeln!(
writer,
"{}",
json_output::diagnostic_to_json(diagnostic, files).unwrap()
)
.unwrap();
} else {
codespan_reporting::term::emit(writer, config, files, diagnostic)
.expect("couldn't emit error to codespan");
match opts.display_style {
Some(opts::DisplayStyle::Json) => {
writeln!(
writer,
"{}",
serde_json::to_string(&json_output::diagnostic_to_json(diagnostic, files)).unwrap()
)
.unwrap();
}

Some(opts::DisplayStyle::Json2) => {
writeln!(
writer,
"{}",
serde_json::to_string(&json_output::JsonOutput::Diagnostic(
json_output::diagnostic_to_json(diagnostic, files)
))
.unwrap()
)
.unwrap();
}

Some(opts::DisplayStyle::Rich) | Some(opts::DisplayStyle::Quiet) | None => {
codespan_reporting::term::emit(writer, config, files, diagnostic)
.expect("couldn't emit error to codespan");
}
}
}

Expand Down
1 change: 1 addition & 0 deletions selene/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ arg_enum! {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DisplayStyle {
Json,
Json2,
Rich,
Quiet,
}
Expand Down

0 comments on commit 716e13b

Please sign in to comment.