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

CodeEditor: Ensure suggestions only apply to the instance of the edit… #70067

Merged
merged 1 commit into from
Jun 14, 2023
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
17 changes: 10 additions & 7 deletions packages/grafana-ui/src/components/Monaco/CodeEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Props = CodeEditorProps & Themeable2;
class UnthemedCodeEditor extends PureComponent<Props> {
completionCancel?: monacoType.IDisposable;
monaco?: Monaco;
modelId?: string;

constructor(props: Props) {
super(props);
Expand Down Expand Up @@ -44,8 +45,8 @@ class UnthemedCodeEditor extends PureComponent<Props> {
return;
}

if (getSuggestions) {
this.completionCancel = registerSuggestions(this.monaco, language, getSuggestions);
if (getSuggestions && this.modelId) {
this.completionCancel = registerSuggestions(this.monaco, language, getSuggestions, this.modelId);
}
}

Expand Down Expand Up @@ -85,21 +86,23 @@ class UnthemedCodeEditor extends PureComponent<Props> {

handleBeforeMount = (monaco: Monaco) => {
this.monaco = monaco;
const { language, getSuggestions, onBeforeEditorMount } = this.props;

if (getSuggestions) {
this.completionCancel = registerSuggestions(monaco, language, getSuggestions);
}
const { onBeforeEditorMount } = this.props;

onBeforeEditorMount?.(monaco);
};

handleOnMount = (editor: MonacoEditorType, monaco: Monaco) => {
const { onChange, onEditorDidMount } = this.props;
const { getSuggestions, language, onChange, onEditorDidMount } = this.props;

this.modelId = editor.getModel()?.id;
this.getEditorValue = () => editor.getValue();

if (getSuggestions && this.modelId) {
this.completionCancel = registerSuggestions(monaco, language, getSuggestions, this.modelId);
}
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, this.onSave);

const languagePromise = this.loadCustomLanguage();

if (onEditorDidMount) {
Expand Down
8 changes: 7 additions & 1 deletion packages/grafana-ui/src/components/Monaco/suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ function mapKinds(monaco: Monaco, sug?: CodeEditorSuggestionItemKind): monacoTyp
export function registerSuggestions(
monaco: Monaco,
language: string,
getSuggestions: CodeEditorSuggestionProvider
getSuggestions: CodeEditorSuggestionProvider,
modelId: string
): monacoType.IDisposable | undefined {
if (!language || !getSuggestions) {
return undefined;
Expand All @@ -82,6 +83,11 @@ export function registerSuggestions(
triggerCharacters: ['$'],

provideCompletionItems: (model, position, context) => {
// only return these suggestions for the specified modelId
// prevents duplicate suggestions when multiple editors are open
if (model.id !== modelId) {
return undefined;
}
const range = {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
Expand Down