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

Oe property work #1763

Merged
merged 3 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions tools/online_editor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ function main() {
editor.onNewPropertyData = (binding_text_provider, p) => {
properties.set_properties(binding_text_provider, p);
};

properties.on_goto_position = (
uri: string,
pos: TextPosition | TextRange,
) => {
editor.goto_position(uri, pos);
};

return properties;
},
{ mode: "tab-after", ref: "Welcome" },
Expand Down
3 changes: 2 additions & 1 deletion tools/online_editor/src/lsp_integration.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial

import { Position, Range } from 'vscode-languageserver-types'
import { Position, Range } from "vscode-languageserver-types";

export interface DeclarationPosition {
uri: string;
Expand All @@ -15,6 +15,7 @@ export interface DefinitionPosition {

export interface Property {
name: string;
group: string;
type_name: string;
declared_at: DeclarationPosition | null;
defined_at: DefinitionPosition | null;
Expand Down
70 changes: 64 additions & 6 deletions tools/online_editor/src/properties_widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

// cSpell: ignore lumino

import { GotoPositionCallback, TextRange } from "./text";

import { Message } from "@lumino/messaging";
import { Widget } from "@lumino/widgets";

Expand All @@ -13,7 +15,32 @@ import {
PropertyQuery,
} from "./lsp_integration";

const TYPE_PATTERN = /^[a-z-]+$/i;

function type_class_for_typename(name: string): string {
if (name === "callback" || name.slice(0, 9) === "callback(") {
return "type-callback";
}
if (name.slice(0, 5) === "enum ") {
return "type-enum";
}
if (name.slice(0, 9) === "function(") {
return "type-function";
}
if (name === "element ref") {
return "type-element-ref";
}
if (TYPE_PATTERN.test(name)) {
return "type-" + name;
}
return "type-unknown";
}

export class PropertiesWidget extends Widget {
#onGotoPosition: GotoPositionCallback = (_u, _p) => {
return;
};

static createNode(): HTMLElement {
const node = document.createElement("div");
const content = document.createElement("div");
Expand Down Expand Up @@ -54,6 +81,10 @@ export class PropertiesWidget extends Widget {
this.dispose();
}

set on_goto_position(callback: GotoPositionCallback) {
this.#onGotoPosition = callback;
}

protected get contentNode(): HTMLDivElement {
return this.node.getElementsByTagName("div")[0] as HTMLDivElement;
}
Expand Down Expand Up @@ -85,12 +116,27 @@ export class PropertiesWidget extends Widget {
private populate_table(
binding_text_provider: BindingTextProvider,
properties: Property[],
uri: string,
) {
const table = this.tableNode;

let current_group = "";

table.innerHTML = "";

for (const p of properties) {
if (p.group !== current_group) {
const group_header = document.createElement("tr");
group_header.className = "group-header";

const group_cell = document.createElement("td");
group_cell.innerText = p.group;
group_cell.setAttribute("colspan", "2");
current_group = p.group;

group_header.appendChild(group_cell);
table.appendChild(group_header);
}
const row = document.createElement("tr");
row.className = "property";
if (p.declared_at == null) {
Expand All @@ -105,13 +151,10 @@ export class PropertiesWidget extends Widget {
name_field.innerText = p.name;
row.appendChild(name_field);

const type_field = document.createElement("td");
type_field.className = "type-column";
type_field.innerText = p.type_name;
row.appendChild(type_field);

const value_field = document.createElement("td");
value_field.className = "value-column";
value_field.classList.add(type_class_for_typename(p.type_name));
value_field.setAttribute("title", p.type_name);
if (p.defined_at != null) {
value_field.innerText = binding_text_provider.binding_text(
p.defined_at,
Expand All @@ -120,6 +163,17 @@ export class PropertiesWidget extends Widget {
value_field.innerText = "";
}
row.appendChild(value_field);
if (p.defined_at != null) {
const r = p.defined_at.expression_range;
row.addEventListener("click", () =>
this.#onGotoPosition(uri, {
startLineNumber: r.start.line + 1,
startColumn: r.start.character + 1,
endLineNumber: r.end.line + 1,
endColumn: r.end.character + 1,
} as TextRange),
);
}

table.appendChild(row);
}
Expand All @@ -130,6 +184,10 @@ export class PropertiesWidget extends Widget {
properties: PropertyQuery,
) {
this.set_header(properties.element);
this.populate_table(binding_text_provider, properties.properties);
this.populate_table(
binding_text_provider,
properties.properties,
properties.source_uri,
);
}
}
15 changes: 11 additions & 4 deletions tools/online_editor/styles/content.css
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@
white-space: nowrap;
}

.properties-editor .type-column {
white-space: nowrap;
}

.properties-editor .value-column {
width: 90%;
}
Expand All @@ -136,10 +132,21 @@
width: 100%;
}

.properties-editor .properties-table .group-header td {
background-color: #000000;
color: #ffffff;
font-weight: bold;
}

.properties-editor .properties-table .undefined {
background-color: #b0b0b0;
}

.properties-editor .value-column.type-unknown:before {
content: "??? ";
padding: 2px;
}

.welcome > div {
padding: 1ex
}
Expand Down