|
| 1 | +/// Python language support plugin for Lumide IDE. |
| 2 | +/// |
| 3 | +/// Registers Pyright as the language server for Python files. |
| 4 | +library; |
| 5 | + |
| 6 | +import 'package:lumide_api/lumide_api.dart'; |
| 7 | + |
| 8 | +void main() => PythonPlugin().run(); |
| 9 | + |
| 10 | +class PythonPlugin extends LumidePlugin { |
| 11 | + static const _logPrefix = '🐍'; |
| 12 | + static const _defaultLspCommand = 'pyright-langserver'; |
| 13 | + static const _pluginId = 'python-lsp'; |
| 14 | + static const _languageId = 'python'; |
| 15 | + static const _fileExtensions = ['.py', '.pyi']; |
| 16 | + static const _restartCommandId = 'lumide_python.restartLsp'; |
| 17 | + static const _pluginName = 'Python'; |
| 18 | + static const _lspArgs = ['--stdio']; |
| 19 | + |
| 20 | + String _lspCommand = _defaultLspCommand; |
| 21 | + |
| 22 | + @override |
| 23 | + Future<void> onActivate(LumideContext context) async { |
| 24 | + log('$_logPrefix $_pluginName plugin activated'); |
| 25 | + |
| 26 | + final customPath = |
| 27 | + await context.workspace.getConfiguration('$_pluginId.path') as String?; |
| 28 | + if (customPath != null && customPath.trim().isNotEmpty) { |
| 29 | + _lspCommand = customPath.trim(); |
| 30 | + } |
| 31 | + |
| 32 | + try { |
| 33 | + final checkCommand = |
| 34 | + _lspCommand == _defaultLspCommand ? 'pyright' : _lspCommand; |
| 35 | + final res = await context.shell.run(checkCommand, ['--version']); |
| 36 | + |
| 37 | + if (res.exitCode != 0) { |
| 38 | + if (!res.stderr.contains('Connection input stream is not set')) { |
| 39 | + throw Exception('Command not found or failed'); |
| 40 | + } |
| 41 | + } |
| 42 | + } catch (e) { |
| 43 | + await context.window.showMessage( |
| 44 | + '$e', |
| 45 | + // '$_lspCommand is not installed. Please install it to enable $_pluginName language support.', |
| 46 | + title: _pluginName, |
| 47 | + type: MessageType.warning, |
| 48 | + ); |
| 49 | + log('$_logPrefix $_lspCommand not found, aborting'); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + await context.languages.registerLanguageServer( |
| 54 | + id: _pluginId, |
| 55 | + languageId: _languageId, |
| 56 | + fileExtensions: _fileExtensions, |
| 57 | + command: _lspCommand, |
| 58 | + args: _lspArgs, |
| 59 | + ); |
| 60 | + |
| 61 | + await context.commands.registerCommand( |
| 62 | + id: _restartCommandId, |
| 63 | + title: '$_pluginName: Restart Language Server', |
| 64 | + category: _pluginName, |
| 65 | + callback: ([args]) async { |
| 66 | + await context.languages.registerLanguageServer( |
| 67 | + id: _pluginId, |
| 68 | + languageId: _languageId, |
| 69 | + fileExtensions: _fileExtensions, |
| 70 | + command: _lspCommand, |
| 71 | + args: _lspArgs, |
| 72 | + ); |
| 73 | + await context.window.showMessage( |
| 74 | + '$_pluginName language server restarted', |
| 75 | + title: _pluginName, |
| 76 | + ); |
| 77 | + }, |
| 78 | + ); |
| 79 | + |
| 80 | + log('$_logPrefix $_pluginName registered for $_languageId files'); |
| 81 | + } |
| 82 | + |
| 83 | + @override |
| 84 | + Future<void> onDeactivate() async { |
| 85 | + log('$_logPrefix $_pluginName plugin deactivated'); |
| 86 | + } |
| 87 | +} |
0 commit comments