Skip to content

Commit

Permalink
feat: add tolgeeHighlight
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Jan 29, 2024
1 parent 834ce2a commit 35b78d2
Show file tree
Hide file tree
Showing 14 changed files with 320 additions and 59 deletions.
166 changes: 164 additions & 2 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@typescript-eslint/parser": "^6.14.0",
"@uiw/react-codemirror": "^4.21.21",
"@vitejs/plugin-react": "^4.2.1",
"concurrently": "^8.2.2",
"eslint": "^8.55.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.5",
Expand Down
3 changes: 2 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { EditorMulti } from "./editor/EditorMulti";

function App() {
const [value, setValue] = useState(
`{value, plural, one {I have # apple} other {I have # apples}}`
`I have {value, number, ::short} items in basket.
This is <i>italic</i>.`
);
const [placeholders, setPlaceholders] = useState(true);

Expand Down
30 changes: 8 additions & 22 deletions src/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,14 @@ import { tolgeeLinter } from "../playground/tolgeeLinter";
import { PlaceholderPlugin } from "../parser/PlaceholderPlugin";
import styled from "@emotion/styled";
import { Placeholder } from "../parser/types";
import { tolgeeHighlight } from "../parser/tolgeeHighlight";
import { tolgeeThemeBase } from "../tolgee-editor";

const StyledEditor = styled("div")`
font-size: 14px;
& .placeholder-widget {
border: 1px solid #2e2e2e4e;
border-radius: 10px;
padding: 1px 4px;
font-size: 12px;
user-select: none;
}
& .placeholder-tagOpen {
border-radius: 10px 0px 0px 10px;
padding-right: 2px;
}
& .placeholder-tagClose {
border-radius: 0px 10px 10px 0px;
padding-left: 2px;
}
& .placeholder-error {
background: #ff000054;
.cm-line {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji",
"Segoe UI Symbol";
}
`;

Expand Down Expand Up @@ -70,6 +54,8 @@ export const Editor: React.FC<Props> = ({
languageCompartment.of(tolgeeSyntax()),
tolgeeLinter,
compartment.current.of([]),
tolgeeThemeBase,
tolgeeHighlight,
],
}),
});
Expand Down
11 changes: 7 additions & 4 deletions src/parser/PlaceholderPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ class PlaceholderWidget extends WidgetType {
this.value = value;
}
toDOM(): HTMLElement {
const span = document.createElement("span");
const spanOuter = document.createElement("span");
let classes = `placeholder-widget placeholder-${this.value.type}`;

if (this.value.error) {
classes =
classes + ` placeholder-error placeholder-error-${this.value.error}`;
}
span.className = classes;
span.textContent = this.value.name || "#";
return span;
spanOuter.className = classes;

const spanInner = document.createElement("span");
spanInner.textContent = this.value.name || "#";
spanOuter.appendChild(spanInner);
return spanOuter;
}
}

Expand Down
15 changes: 15 additions & 0 deletions src/parser/getTolgeePlurals.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getTolgeePlurals } from "./getTolgeePlurals";

function getText() {
return expect.getState().currentTestName!.replace("get tolgee plurals ", "");
}

function shouldBePlural() {
expect(getTolgeePlurals(getText())).not.toBeNull();
}

describe("get tolgee plurals", () => {
it("{variable, plural, other {test}}", () => {
shouldBePlural();
});
});
4 changes: 2 additions & 2 deletions src/parser/getTolgeePlurals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import { TolgeeFormat } from "./types";
const REQUIRED_NODES: Record<number, number | undefined> = {
0: Expression,
4: Param,
5: SelectFunction,
6: SelectFunction,
// this also excludes 'offset:x'
6: SelectVariant,
8: SelectVariant,
};

function getNodeText(node: SyntaxNode, input: string) {
Expand Down
9 changes: 5 additions & 4 deletions src/parser/tolgee.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ Expression { ExpressionOpen (FormatExpression | SelectExpression) ExpressionClos

HtmlTag { HtmlTagOpen | HtmlTagClose }
HtmlTagOpen { "<" TagName space? ">" }
HtmlTagClose { "<" "/" TagName space? ">" }
HtmlTagClose { "<" Slash TagName space? ">" }

FormatExpression { space? Param space? ("," space? FormatFunction space? ("," (space? FormatStyle)+ space?)?)? }
SelectExpression { space? Param space? "," space? SelectFunction space? "," (space? Offset)? (space? SelectVariant)+ space? }
FormatExpression { space? Param space? (Sep space? FormatFunction space? (Sep (space? FormatStyle)+ space?)?)? }
SelectExpression { space? Param space? Sep space? SelectFunction space? Sep (space? Offset)? (space? SelectVariant)+ space? }
SelectVariant { VariantDescriptor space? "{" VariantContent "}" }
VariantContent { nested_content? }

Expand All @@ -32,7 +32,8 @@ TagName { tag_name }
"}"
"<"
">"
"/"
Sep { "," }
Slash { "/" }
offset_definition { "offset:" $[+-]? $[0-9]+ }
plural_placeholder { '#' }
// non-White_Space and non-PatternSyntax
Expand Down
16 changes: 16 additions & 0 deletions src/parser/tolgeeHighlight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { tags } from "@lezer/highlight";
import { HighlightStyle, syntaxHighlighting } from "@codemirror/language";

const VARIABLE_COLOR = "#008371";
const TAG_COLOR = "#822343";

export const tolgeeHighlight = syntaxHighlighting(
HighlightStyle.define([
{ tag: tags.variableName, color: VARIABLE_COLOR },
{ tag: tags.keyword, color: VARIABLE_COLOR },
{ tag: tags.paren, color: VARIABLE_COLOR },
{ tag: tags.separator, color: VARIABLE_COLOR },
{ tag: tags.bracket, color: VARIABLE_COLOR },
{ tag: tags.tagName, color: TAG_COLOR },
])
);
Loading

0 comments on commit 35b78d2

Please sign in to comment.