如何在Visual Studio Code中基于项目已打开文件实现自动补全?替代All Autocomplete插件
Got it, I’ve been in your shoes before—trying to get cross-file autocompletion in VS Code without breaking PHP’s native IntelliSense is such a pain, especially since All Autocomplete is more trouble than it’s worth here. Let’s go through some reliable fixes that work:
1. Tweak VS Code’s Built-In IntelliSense
VS Code actually has native support for autocompleting words from open files—you just might need to enable a few settings first:
- Open your settings (press
Ctrl+,orCmd+,on Mac) - Search for editor.suggest.showWords and make sure it’s checked
- Double-check that PHP files are properly associated with the PHP language: search for files.associations and ensure
*.phpis mapped tophp
You can also directly edit your settings.json to add these lines:
{ "editor.suggest.showWords": true, "files.associations": { "*.php": "php" } }
This makes sure VS Code pulls words from all open files while keeping PHP’s language-specific autocompletion intact.
2. Use a PHP-Friendly Autocomplete Plugin
Skip All Autocomplete and go for plugins designed to play nice with PHP’s language server (like Intelephense):
- Workspace Autocomplete: This plugin focuses solely on providing suggestions from files in your current workspace, and it won’t override or break PHP’s native IntelliSense. It complements PHP’s own autocompletion instead of replacing it.
- Intelephense Tuning: If you’re already using Intelephense (the best PHP language server for VS Code), make sure it’s configured to scan all relevant files. Add this to your
settings.json:
Intelephense automatically includes symbols from open files once it’s set up correctly—you might not even need an extra plugin!{ "intelephense.files.include": [ "**/*.php", "**/*.phtml" ], "intelephense.completion.triggerParameterHints": true }
3. (Optional) Block All Autocomplete from PHP Files
If you really can’t part with All Autocomplete (even with its issues), you can tell it to leave PHP files alone. Add this to your settings.json:
{ "allAutocomplete.excludeLanguages": ["php"] }
This way, All Autocomplete works for other file types but doesn’t interfere with PHP’s autocompletion. Just keep in mind the plugin’s vulnerabilities you mentioned—this is a temporary workaround, not a long-term fix.
4. Manual Trigger for Priority Completion
Sometimes VS Code’s autocomplete can get confused about which suggestions to show first. Use Ctrl+Space (or Cmd+Space on Mac) to manually trigger autocomplete—this will prioritize language-specific suggestions (like PHP functions/variables) alongside cross-file word suggestions.
内容的提问来源于stack exchange,提问作者kanlukasz

