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