Skip to content

Commit

Permalink
Merge pull request #286 from RyotaUshio/fix/issue284
Browse files Browse the repository at this point in the history
Add an option to disable snippet expansion when IME is on
  • Loading branch information
artisticat1 authored Apr 27, 2024
2 parents 899cc8b + 4852b29 commit aad8adf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

8 changes: 6 additions & 2 deletions src/latex_suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { concealPlugin } from "./editor_extensions/conceal";
import { LatexSuiteCMSettings } from "./settings/settings";
import { colorPairedBracketsPluginLowestPrec, highlightCursorBracketsPlugin } from "./editor_extensions/highlight_brackets";
import { cursorTooltipBaseTheme, cursorTooltipField } from "./editor_extensions/math_tooltip";
import { isComposing } from "./utils/editor_utils";

export const handleUpdate = (update: ViewUpdate) => {
const cursorTriggeredByChange = update.state.field(cursorTriggerStateField, false);
Expand All @@ -37,12 +38,12 @@ export const handleUpdate = (update: ViewUpdate) => {
}

export const onKeydown = (event: KeyboardEvent, view: EditorView) => {
const success = handleKeydown(event.key, event.shiftKey, event.ctrlKey || event.metaKey, view);
const success = handleKeydown(event.key, event.shiftKey, event.ctrlKey || event.metaKey, isComposing(view, event), view);

if (success) event.preventDefault();
}

export const handleKeydown = (key: string, shiftKey: boolean, ctrlKey: boolean, view: EditorView) => {
export const handleKeydown = (key: string, shiftKey: boolean, ctrlKey: boolean, isIME: boolean, view: EditorView) => {

const settings = getLatexSuiteConfig(view);
const ctx = Context.fromView(view);
Expand All @@ -51,6 +52,9 @@ export const handleKeydown = (key: string, shiftKey: boolean, ctrlKey: boolean,

if (settings.snippetsEnabled) {

// Prevent IME from triggering keydown events.
if (settings.suppressSnippetTriggerOnIME && isIME) return;

// Allows Ctrl + z for undo, instead of triggering a snippet ending with z
if (!ctrlKey) {
try {
Expand Down
2 changes: 2 additions & 0 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DEFAULT_SNIPPET_VARIABLES } from "src/utils/default_snippet_variables";
interface LatexSuiteBasicSettings {
snippetsEnabled: boolean;
snippetsTrigger: "Tab" | " "
suppressSnippetTriggerOnIME: boolean;
removeSnippetWhitespace: boolean;
loadSnippetsFromFile: boolean;
loadSnippetVariablesFromFile: boolean;
Expand Down Expand Up @@ -52,6 +53,7 @@ export const DEFAULT_SETTINGS: LatexSuitePluginSettings = {
// Basic settings
snippetsEnabled: true,
snippetsTrigger: "Tab",
suppressSnippetTriggerOnIME: true,
removeSnippetWhitespace: true,
loadSnippetsFromFile: false,
loadSnippetVariablesFromFile: false,
Expand Down
11 changes: 11 additions & 0 deletions src/settings/settings_tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,17 @@ export class LatexSuiteSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
}));

new Setting(containerEl)
.setName("Don't trigger snippets when IME is active")
.setDesc("Whether to suppress the snippet triggering when an IME is active.")
.addToggle((toggle) => toggle
.setValue(this.plugin.settings.suppressSnippetTriggerOnIME)
.onChange(async (value) => {
this.plugin.settings.suppressSnippetTriggerOnIME = value;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Code languages to interpret as math mode")
.setDesc("Codeblock languages where the whole code block should be treated like a math block, separated by commas.")
Expand Down
12 changes: 12 additions & 0 deletions src/utils/editor_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,15 @@ export function escalateToToken(cursor: TreeCursor, dir: Direction, target: stri

return null;
}


/**
* Check if the user is typing in an IME composition.
* Returns true even if the given event is the first keydown event of an IME composition.
*/
export function isComposing(view: EditorView, event: KeyboardEvent): boolean {
// view.composing and event.isComposing are false for the first keydown event of an IME composition,
// so we need to check for event.keyCode === 229 to prevent IME from triggering keydown events.
// Note that keyCode is deprecated - it is used here because it is apparently the only way to detect the first keydown event of an IME composition.
return view.composing || event.keyCode === 229;
}

0 comments on commit aad8adf

Please sign in to comment.