Skip to content

Commit

Permalink
Synchronize tab settings with monaco
Browse files Browse the repository at this point in the history
Also add the tabstop and expandtab options,
indenting behavior is more predictable

bump to 1.0.3
  • Loading branch information
PollRobots committed Jul 10, 2024
1 parent e6023a1 commit 35da428
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "vim-monaco",
"type": "module",
"version": "1.0.2",
"version": "1.0.3",
"description": "vim support for monaco-editor",
"keywords": [
"monaco-editor",
Expand Down
41 changes: 31 additions & 10 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,27 @@ export default class EditorAdapter {
setOption(key: string, value: string | number | boolean) {
this.state[key] = value;

if (key === "theme") {
this.theme = value as string;
this.editor.updateOptions({
theme: this.theme,
});
switch (key) {
case "theme": {
this.theme = value as string;
this.editor.updateOptions({
theme: this.theme,
});
}
case "indentWithTabs": {
const model = this.editor.getModel()!;
model.updateOptions({ insertSpaces: !value });
break;
}
case "tabSize": {
const tabSize = typeof value === "number" ? value : Number(value);
if (isNaN(tabSize)) {
return;
}
const model = this.editor.getModel()!;
model.updateOptions({ tabSize: tabSize });
break;
}
}
}

Expand All @@ -373,8 +389,14 @@ export default class EditorAdapter {
return this.getConfiguration().readOnly;
case "firstLineNumber":
return this.firstLine() + 1;
case "indentWithTabs":
return !this.getModel_().getOptions().insertSpaces;
case "indentWithTabs": {
const model = this.editor.getModel()!;
return !model.getOptions().insertSpaces;
}
case "tabSize": {
const model = this.editor.getModel()!;
return model.getOptions().tabSize;
}
case "theme":
if (this.theme) {
return this.theme;
Expand Down Expand Up @@ -1121,9 +1143,8 @@ export default class EditorAdapter {
}

indentLine(line: number, indentRight: boolean = true) {
const opts = window.monaco.editor.EditorOption;
const useTabs = this.editor.getOption(opts.useTabStops);
const tabWidth = this.editor.getOption(opts.tabIndex);
const useTabs = !!this.getOption("indentWithTabs");
const tabWidth = Number(this.getOption("tabSize"));
const spaces = "".padEnd(tabWidth, " ");

if (indentRight) {
Expand Down
34 changes: 34 additions & 0 deletions src/keymap_vim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2326,4 +2326,38 @@ defineOption(
}
);

defineOption("expandtab", undefined, "boolean", ["et"], (value, adapter) => {
if (value === undefined) {
if (adapter) {
return !adapter.getOption("indentWithTabs");
}
} else if (adapter) {
adapter.setOption("indentWithTabs", !value);
return !!value;
}
return false;
});

defineOption("tabstop", undefined, "number", ["ts"], (value, adapter) => {
if (value === undefined) {
if (adapter) {
const current = adapter.getOption("tabSize");
if (typeof current === "number") {
return current;
} else if (!isNaN(Number(current))) {
return Number(current);
}
}
} else if (adapter) {
if (typeof value !== "number") {
value = Number(value);
}
if (!isNaN(value)) {
adapter.setOption("tabSize", value);
return value;
}
}
return 8;
});

export const vimApi = new VimApi();

0 comments on commit 35da428

Please sign in to comment.