iamcco / coc-flutter

flutter support for (Neo)vim
472 stars 38 forks source link

Package development: Example Projects don't get a working language server / code actions #134

Closed dkbast closed 2 years ago

dkbast commented 3 years ago

Describe the bug I am developing a new Flutter package - when I'm in package_name/lib everything works as expected, but when I'm writing the examples in package_name/example/lib/main.dart I don't get any code actions or language server features.

To Reproduce Steps to reproduce the behavior:

  1. For reference go to https://flutter.dev/docs/development/packages-and-plugins/developing-packages
  2. Create a package like os: flutter create --template=package hello
  3. Create a new folder 'example' and run flutter create .
  4. vim example/lib/main.dart

Expected behavior I can develop the example using all the features I usually have - It would be ok if I would need two different sessions for developing the example and the package - the entry point could determine where the language server should start.

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

Config If you have any config for this specific extenstion in you coc-settings please include them here.

{
  "languageserver": {
    "dart": {
      "command": "dart_language_server",
      "args": [],
      "filetypes": ["dart"],
      "initializationOptions": {},
      "settings": {
        "dart": {
          "validation": {},
          "completion": {}
        }
      }
    }
  }
}
Kavantix commented 3 years ago

Can you try opening the project from the example directory itself. Also, you should not need the part in ‘coc-settings`, coc-flutter handles that

dkbast commented 3 years ago

There seems to be no difference between vim example/lib/main.dart and cd example vim lib/main.dart

In both cases I get '[coc.nvim] No code action available' and no analyzer, closing tags etc pp

The path within vim is displayed as 'example/lib/main.dart' no matter from where I start - so I guess there is some magic going on in discovering which kind of project I'm working on and to auto discover other files and folders?

changyiyao commented 3 years ago

try setting flutter.lsp.initialization.onlyAnalyzeProjectsWithOpenFiles to false

dkbast commented 3 years ago

@changyiyao I tried, but this didn't work for me - did you give it a try? Does it work for you?

changyiyao commented 3 years ago

@dkbast Have the similar issue but workaround well as the following:

  1. make the package be the local dependency of the example project, you may configure like ../ on dependency_overrides to the pubspec of example.

  2. cd to example dir in your terminal emulator.

  3. open vim/nvim editor without args.

  4. :e lib/main.dart (In my experience it must firstly open one dart file which belongs to example project, otherwise the lsp doesn't work well for dart files of example project if you open a dart file of package project at once)

  5. At last, with cocconfig I mentioned above, you will see the lsp works both in example and package project.

Kavantix commented 3 years ago

@dkbast can you check if v1.9.6 fixed your issue?

changyiyao commented 3 years ago

@dkbast Have the similar issue but workaround well as the following:

  1. make the package be the local dependency of the example project, you may configure like ../ on dependency_overrides to the pubspec of example.
  2. cd to example dir in your terminal emulator.
  3. open vim/nvim editor without args.
  4. :e lib/main.dart (In my experience it must firstly open one dart file which belongs to example project, otherwise the lsp doesn't work well for dart files of example project if you open a dart file of package project at once)
  5. At last, with cocconfig I mentioned above, you will see the lsp works both in example and package project.

Finally, I found another way to work properly without cd into the example folder:

Background: The whole project structure may be like:

. (package project root)
├── CHANGELOG.md
├── LICENSE
├── README.md
├── example
│   ├── README.md
│   ├── android/
│   ├── example.iml
│   ├── ios/
│   ├── lib/main.dart
│   ├── pubspec.lock
│   ├── pubspec.yaml
│   ├── test/
├── lib/
├── pubspec.lock
├── pubspec.yaml
└── test/

Since default rootPatterns of workspace when you open dart files is like: {"global": [".git", ".hg", ".projections.json"], "buffer": [], "server": ["pubspec.yaml"]} Coc.nvim recognize workspaces by this patterns when you once open a file. By default, when opening example/lib/main.dart from package project root path, Coc use up-bottom mode to specify the workspace, which only treats the package project root path as the workspace rather than the example folder, which causes commands such as flutter.run cannot found the entry lib/main.dart file, even though you append args -t example/lib/main.dart.

To solve the issue:

  1. Set workspace.bottomUpFiletypes, such as
    {
    ...
    "workspace.bottomUpFiletypes": ["dart", "yaml"],
    ...
    }
  2. Enter (neo)vim from package project root path, then open a dart file or yaml file under example folder, then the flutter commands works well.

PS: Legacy issues: no exclude options for rootPattern of coc's workspace and flutter.workspaceFolder.ignore cannot be tranferred to it(coc client side), CocList files/grep can include the ~/.pub-cache/** which is declared in flutter.workspaceFolder.ignore

dkbast commented 2 years ago

Sorry for not being very responsive on this issue - today I opened up another package and it looks like this problem does not exist anymore. @Kavantix looks like this is fixed without needing any configuration. Thanks!

For others wondering if this is working, I've just checked out https://github.com/trufi-association/trufi-core and and can confirm that I can work in the package as well as in the example directory, at least at first glance I didn't run into any issues.