Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 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 @@ -30,6 +30,7 @@
"@tauri-apps/plugin-os": "^2.3.2",
"@tauri-apps/plugin-shell": "^2.3.3",
"@tauri-apps/plugin-store": "^2.4.1",
"antlr4": "^4.13.2",
"debug": "^4.4.3",
"highlight.js": "^11.11.1",
"json-with-bigint": "^3.5.2",
Expand Down
197 changes: 45 additions & 152 deletions src/common/monaco/completion.ts
Original file line number Diff line number Diff line change
@@ -1,158 +1,51 @@
import * as monaco from 'monaco-editor';
import { paths } from './keywords.ts';
import { searchTokens } from './tokenlizer.ts';
import { getSubDsqlTree } from './dsql';

const providePathCompletionItems = (lineContent: string) => {
const methods = new Map<RegExp, string>([
[/^ge?t?$/gi, 'GET '],
[/^put?$/gi, 'PUT '],
[/^pos?t?$/gi, 'POST '],
[/^de?l?e?t?e?$/gi, 'DELETE '],
]);
const matchedMethodKey = Array.from(methods.keys()).find(regex => regex.test(lineContent));
if (matchedMethodKey) {
const method = methods.get(matchedMethodKey);
return {
suggestions: [
{
label: method,
kind: monaco.languages.CompletionItemKind.Constant,
insertText: method,
},
],
};
}
const isPathMatch = /^(GET|POST|PUT|DELETE)(\s+[a-zA-Z0-9_\/-?\-&,*]*)$/.test(lineContent);
const word = lineContent.split(/[ /]+/).pop() || '';
if (isPathMatch) {
return {
suggestions: paths
.filter(p => p.startsWith(word))
.map(keyword => ({
label: keyword,
kind: monaco.languages.CompletionItemKind.Unit,
insertText: keyword,
})),
};
}
};

const getQueryTreePath = (actionBlockContent: string) => {
const pathStack: string[] = [];
actionBlockContent
.replace(/['"]/g, '')
.replace(/\/\/.*?\n|\/\*[\s\S]*?\*\//g, '')

.split(/[{\[]/)
.forEach(item => {
const pureItem = item.replace(/\s+/g, '');

/[}\]]/.test(pureItem) && pathStack.pop();
/[\w.]+:$/.test(pureItem) && pathStack.push(pureItem.split(',').pop() || '');
});

return pathStack.map(path => path.replace(/[:},\s]+/g, ''));
import {
grammarCompletionProvider,
setCompletionConfig,
setDynamicOptions,
BackendType,
} from './grammar';

// Re-export types for external use
export { BackendType };
export type { CompletionConfig, DynamicCompletionOptions } from './grammar';

/**
* Configure the completion engine for a specific backend and version
* This affects the available endpoints, query types, and features
*/
export const configureCompletions = (config: {
backend: 'elasticsearch' | 'opensearch';
version?: string;
}): void => {
const backendType =
config.backend === 'opensearch' ? BackendType.OPENSEARCH : BackendType.ELASTICSEARCH;
setCompletionConfig({
backend: backendType,
version: config.version,
});
};

const provideQDSLCompletionItems = (
textUntilPosition: string,
lineContent: string,
position: monaco.Position,
/**
* Configure dynamic completion options from the connected database
* These options provide real values for path parameters like {index}, {repository}, {template}
*
* @param options - Dynamic options object containing:
* - activeIndex: The currently selected index from toolbar
* - indices: All available indices in the cluster
* - repositories: Available snapshot repositories
* - templates: Available index templates
* - pipelines: Available ingest pipelines
* - aliases: Available index aliases
*/
export const configureDynamicOptions = setDynamicOptions;

/**
* Grammar-driven completion provider for Elasticsearch/OpenSearch queries
*/
export const searchCompletionProvider = (
model: monaco.editor.ITextModel,
position: monaco.Position,
) => {
// const word = textUntilPosition.split(/[ /]+/).pop() || '';
const closureIndex = isReplaceCompletion(lineContent, textUntilPosition);

const action = searchTokens.find(
({ position: { startLineNumber, endLineNumber } }) =>
position.lineNumber > startLineNumber && position.lineNumber < endLineNumber,
);
if (!action) {
return;
}

const actionBlockContent = model.getValueInRange({
startLineNumber: action?.position.startLineNumber + 1,
endLineNumber: position.lineNumber,
startColumn: 1,
endColumn: position.column,
});
const queryAction = action.path.split('/')?.pop()?.replace(/\?.*/g, '');

if (!queryAction) {
return;
}

const queryTreePath = getQueryTreePath(actionBlockContent);
const dsqlSubTree = getSubDsqlTree(queryAction, queryTreePath);
if (!dsqlSubTree) {
return;
}

const suggestions = Object.entries(dsqlSubTree?.children ?? {})
.filter(([key]) => key !== '*')
.map(([, value]) => ({
label: value.label,
kind: monaco.languages.CompletionItemKind.Keyword,
...{
insertText: closureIndex < 0 ? value.snippet : value.label,
insertTextRules:
closureIndex < 0
? monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet
: monaco.languages.CompletionItemInsertTextRule.None,
range:
closureIndex < 0
? undefined
: new monaco.Range(
position.lineNumber,
position.column,
position.lineNumber,
closureIndex,
),
},
}));

return { suggestions };
};

const isReplaceCompletion = (lineContent: string, textUntilPosition: string) => {
const matches = lineContent?.substring(textUntilPosition.length)?.match(/[,":]/);
if (matches && matches[0]) {
return (
textUntilPosition.length +
lineContent?.substring(textUntilPosition.length).indexOf(matches[0]) +
1
);
} else {
return -1;
}
};

const searchCompletionProvider = (model: monaco.editor.ITextModel, position: monaco.Position) => {
const textUntilPosition = model.getValueInRange({
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: 1,
endColumn: position.column,
});
const lineContent = model.getLineContent(position.lineNumber);

const methodCompletions = providePathCompletionItems(textUntilPosition);
if (methodCompletions) {
return methodCompletions;
}

const keywordCompletions = provideQDSLCompletionItems(
textUntilPosition,
lineContent,
position,
model,
);

if (keywordCompletions) {
return keywordCompletions;
}
return grammarCompletionProvider(model, position);
};

export { searchCompletionProvider };
Loading