Files
FastGPT/packages/web/components/common/Textarea/CodeEditor/usePythonCompletion.ts
Theresa 2d3117c5da feat: update ESLint config with @typescript-eslint/consistent-type-imports (#4746)
* update: Add type

* fix: update import statement for NextApiRequest type

* fix: update imports to use type for LexicalEditor and EditorState

* Refactor imports to use 'import type' for type-only imports across multiple files

- Updated imports in various components and API files to use 'import type' for better clarity and to optimize TypeScript's type checking.
- Ensured consistent usage of type imports in files related to chat, dataset, workflow, and user management.
- Improved code readability and maintainability by distinguishing between value and type imports.

* refactor: remove old ESLint configuration and add new rules

- Deleted the old ESLint configuration file from the app project.
- Added a new ESLint configuration file with updated rules and settings.
- Changed imports to use type-only imports in various files for better clarity and performance.
- Updated TypeScript configuration to remove unnecessary options.
- Added an ESLint ignore file to exclude build and dependency directories from linting.

* fix: update imports to use 'import type' for type-only imports in schema files
2025-05-06 17:33:09 +08:00

84 lines
2.6 KiB
TypeScript

import { type Monaco } from '@monaco-editor/react';
import { useCallback } from 'react';
let monacoInstance: Monaco | null = null;
const usePythonCompletion = () => {
return useCallback((monaco: Monaco) => {
if (monacoInstance === monaco) return;
monacoInstance = monaco;
monaco.languages.registerCompletionItemProvider('python', {
provideCompletionItems: (model, position) => {
const wordInfo = model.getWordUntilPosition(position);
const currentWordPrefix = wordInfo.word;
const lineContent = model.getLineContent(position.lineNumber);
const range = {
startLineNumber: position.lineNumber,
endLineNumber: position.lineNumber,
startColumn: wordInfo.startColumn,
endColumn: wordInfo.endColumn
};
const baseSuggestions = [
{
label: 'len',
kind: monaco.languages.CompletionItemKind.Function,
insertText: 'len()',
documentation: 'get length of object',
range,
sortText: 'a'
}
];
const filtered = baseSuggestions.filter((item) =>
item.label.toLowerCase().startsWith(currentWordPrefix.toLowerCase())
);
if (lineContent.startsWith('import')) {
const importLength = 'import'.length;
const afterImport = lineContent.slice(importLength);
const spaceMatch = afterImport.match(/^\s*/);
const spaceLength = spaceMatch ? spaceMatch[0].length : 0;
const startReplaceCol = importLength + spaceLength + 1;
const currentCol = position.column;
const replaceRange = new monaco.Range(
position.lineNumber,
startReplaceCol,
position.lineNumber,
currentCol
);
const needsSpace = spaceLength === 0;
return {
suggestions: [
{
label: 'numpy',
kind: monaco.languages.CompletionItemKind.Module,
insertText: `${needsSpace ? ' ' : ''}numpy as np`,
documentation: 'numerical computing library',
range: replaceRange,
sortText: 'a'
},
{
label: 'pandas',
kind: monaco.languages.CompletionItemKind.Module,
insertText: `${needsSpace ? ' ' : ''}pandas as pd`,
documentation: 'data analysis library',
range: replaceRange
}
]
};
}
return { suggestions: filtered };
},
triggerCharacters: ['.', '_']
});
}, []);
};
export default usePythonCompletion;