Skip to content

Commit

Permalink
feat(playground): add lineage panel
Browse files Browse the repository at this point in the history
  • Loading branch information
gvozdvmozgu authored and benfdking committed Sep 17, 2024
1 parent e9b84b3 commit abc267b
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 30 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/lib-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ line-index = "0.1.1"
sqruff-lib.workspace = true
sqruff-lib-core = { path = "../lib-core", features = ["stringify"] }
wasm-bindgen = "0.2"
lineage = { path = "../lineage" }
ahash.workspace = true
65 changes: 58 additions & 7 deletions crates/lib-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use ahash::AHashMap;
use line_index::LineIndex;
use lineage::{Lineage, Node};
use sqruff_lib::core::config::FluffConfig;
use sqruff_lib::core::linter::core::Linter as SqruffLinter;
use sqruff_lib_core::parser::parser::Parser;
use sqruff_lib_core::parser::segments::base::Tables;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -32,7 +35,7 @@ pub struct Linter {
pub enum Tool {
Format = "Format",
Cst = "Cst",
None = "None",
Lineage = "Lineage",
}

#[wasm_bindgen]
Expand Down Expand Up @@ -79,10 +82,7 @@ impl Linter {
let tables = Tables::default();
let parsed = self.base.parse_string(&tables, sql, None, None).unwrap();

let mut cst = None;
if tool == Tool::Cst {
cst = parsed.tree.clone();
}
let cst = if tool == Tool::Cst { parsed.tree.clone() } else { None };

let mut result = self.base.lint_parsed(&tables, parsed, rule_pack, tool == Tool::Format);
let violations = &mut result.violations;
Expand All @@ -106,10 +106,61 @@ impl Linter {
let secondary = match tool {
Tool::Format => result.fix_string(),
Tool::Cst => cst.unwrap().stringify(false),
Tool::None => String::new(),
Tool::__Invalid => String::new(),
Tool::Lineage => {
let parser = Parser::new(self.base.config().get_dialect(), AHashMap::new());
let (tables, node) = Lineage::new(parser, "", sql).build();

print_tree(&tables, node, "", "", "")
}
Tool::__Invalid => String::from("Error: unsupported tool"),
};

Result { diagnostics, secondary }
}
}

fn print_tree(
tables: &lineage::ir::Tables,
node: Node,
parent_prefix: &str,
immediate_prefix: &str,
parent_suffix: &str,
) -> String {
use std::fmt::Write;

let node_data = &tables.nodes[node];

let name = &node_data.name;
let source = tables.stringify(node_data.source);
let expression = tables.stringify(node_data.expression);
let source_name = &node_data.source_name;
let reference_node_name = &&node_data.reference_node_name;

let mut string = String::new();

let _ = writeln!(string, "{:1$}{parent_prefix}{immediate_prefix}name: {name}", "", 0);
let _ = writeln!(string, "{:1$}{parent_prefix}{immediate_prefix}source: {source}", "", 0);
let _ =
writeln!(string, "{:1$}{parent_prefix}{immediate_prefix}expression: {expression}", "", 0);
let _ =
writeln!(string, "{:1$}{parent_prefix}{immediate_prefix}source_name: {source_name}", "", 0);
let _ = writeln!(
string,
"{:1$}{parent_prefix}{immediate_prefix}reference_node_name: {reference_node_name}",
"", 0
);

let mut it = node_data.downstream.iter().peekable();
let child_prefix = format!("{0}{1}", parent_prefix, parent_suffix);

while let Some(child) = it.next().copied() {
let ret = match it.peek() {
None => print_tree(tables, child, &child_prefix, "└─ ", " "),
Some(_) => print_tree(tables, child, &child_prefix, "├─ ", "│ "),
};

string.push_str(&ret);
}

string
}
6 changes: 5 additions & 1 deletion crates/lib/src/core/linter/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ impl Linter {

let mut buffer = Vec::new();
let mut ignores = AHashMap::new();
let sql_file_exts = self.config.sql_file_exts(); // Replace with actual extensions
let sql_file_exts = self.config.sql_file_exts();

for (dirpath, _, filenames) in path_walk {
for fname in filenames {
Expand Down Expand Up @@ -595,6 +595,10 @@ impl Linter {
files
}

pub fn config(&self) -> &FluffConfig {
&self.config
}

pub fn config_mut(&mut self) -> &mut FluffConfig {
&mut self.config
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lineage/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Tables {
dummy
}

pub(crate) fn stringify(&self, expr: Expr) -> String {
pub fn stringify(&self, expr: Expr) -> String {
match &self.exprs[expr].kind {
ExprKind::Function(callee, args) => {
let args =
Expand Down
2 changes: 1 addition & 1 deletion crates/lineage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use sqruff_lib_core::parser::parser::Parser;
use sqruff_lib_core::parser::segments::base::ErasedSegment;

mod expand;
mod ir;
pub mod ir;
mod qualify;
mod schema;
mod scope;
Expand Down
17 changes: 17 additions & 0 deletions playground/src/Editor/Icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,20 @@ export function StructureIcon() {
</svg>
);
}

export function LineageIcon() {
return (
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M13 3.5C13 2.119 10.761 1 8 1C5.239 1 3 2.119 3 3.5C3 3.54 3.02 3.577 3.024 3.617H3V12.489L3.056 12.846C3.336 14.056 5.429 15 8 15C10.571 15 12.664 14.056 12.944 12.846L13 12.489V3.617H12.976C12.98 3.577 13 3.54 13 3.5ZM8 2.032C10.442 2.032 12 2.996 12 3.5C12 4.004 10.442 4.968 8 4.968C5.558 4.968 4 4 4 3.5C4 3 5.558 2.032 8 2.032ZM12 12.49L11.97 12.621C11.855 13.116 10.431 14 8 14C5.569 14 4.145 13.116 4.03 12.621L4 12.49V4.99C5.21092 5.69833 6.59796 6.04855 8 6C9.40243 6.04734 10.7895 5.69572 12 4.986V12.486V12.49Z"
fill="#C5C5C5"
/>
</svg>
);
}
39 changes: 20 additions & 19 deletions playground/src/Editor/SecondaryPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@ import MonacoEditor from "@monaco-editor/react";
export enum SecondaryTool {
"Format" = "Format",
"Cst" = "Cst",
"Lineage" = "Lineage",
}

export default function SecondaryPanel({ result }: { result: string }) {
export default function SecondaryPanel({
tool,
result,
}: {
tool: SecondaryTool;
result: string;
}) {
return (
<div className="flex flex-col h-full">
<div className="flex-grow">
<Content result={result} />
<MonacoEditor
options={{
readOnly: true,
minimap: { enabled: false },
fontSize: 14,
roundedSelection: false,
scrollBeyondLastLine: false,
contextmenu: false,
}}
language={tool === "Format" ? "sql" : "yml"}
value={result}
/>
</div>
</div>
);
}

function Content({ result }: { result: string }) {
return (
<MonacoEditor
options={{
readOnly: true,
minimap: { enabled: false },
fontSize: 14,
roundedSelection: false,
scrollBeyondLastLine: false,
contextmenu: false,
}}
language={"sql"}
value={result}
/>
);
}
11 changes: 10 additions & 1 deletion playground/src/Editor/SecondarySideBar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import SideBar, { SideBarEntry } from "./SideBar";
import { FormatIcon, StructureIcon } from "./Icons";
import { FormatIcon, LineageIcon, StructureIcon } from "./Icons";
import { SecondaryTool } from "./SecondaryPanel";

interface RightSideBarProps {
Expand Down Expand Up @@ -30,6 +30,15 @@ export default function SecondarySideBar({
>
<StructureIcon />
</SideBarEntry>

<SideBarEntry
title="Lineage"
position={"right"}
selected={selected === SecondaryTool.Lineage}
onClick={() => onSelected(SecondaryTool.Lineage)}
>
<LineageIcon />
</SideBarEntry>
</SideBar>
);
}

0 comments on commit abc267b

Please sign in to comment.