Files
FastGPT/packages/web/components/common/Textarea/CodeEditor/usePythonCompletion.ts
Archer 565a966d19 Python Sandbox (#4380)
* Python3 Sandbox (#3944)

* update python box (#4251)

* update python box

* Adjust the height of the NodeCode border.

* update python sandbox and add test systemcall bash

* update sandbox

* add VERSION_RELEASE (#4376)

* save empty docx

* fix pythonbox log error

* fix: js template

---------

Co-authored-by: dogfar <37035781+dogfar@users.noreply.github.com>
Co-authored-by: gggaaallleee <91131304+gggaaallleee@users.noreply.github.com>
Co-authored-by: gggaaallleee <1293587368@qq.com>
2025-03-28 13:45:09 +08:00

84 lines
2.6 KiB
TypeScript

import { 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;