Search code examples
visual-studio-codefortranpetsc

Fortran declarations using PETSc's key words not recognized in VSCode


I'm using VSCode with Modern Fortran+fortls to write Fortran code with PETSc library. The declaration using PETSc's key words seems not being recognized at all. I tried to include the path of PETSc library and header files in .fortls file using include_dirs etc, but it does not resolve the issue. Can anyone give me a hint so the all the keywords and function declarations in PETSc can be handled by fortls? Thanks a lot

I used the following in the .fortls file

{
  "source_dirs": ["./**","./src/**",                                                                
    "/Users/apple/local/petsc/osx-gfc/include/**",                                                   
    "/Users/apple/local/petsc/include/**",                                                           
    "/Users/apple/local/petsc/include/petsc/finclude",                                               
    "/Users/apple/local/petsc/src/vec/f90-mod",                                                      
    "/Users/apple/local/petsc/src/ksp/f90-mod",                                                      
    "/Users/apple/local/petsc/src/vec/f90-mod",                                                      
    "/Users/apple/local/petsc/src/dm/f90-mod",                                                       
    "/Users/apple/local/petsc/src/mat/f90-mod"                                                       
  ],                                                                                                
  "include_dirs": ["./include/**",                                                                  
    "/Users/apple/local/petsc/osx-gfc/include/**",                                                   
    "/Users/apple/local/petsc/include/**",                                                           
    "/Users/apple/local/petsc/include/petsc/finclude/",                                              
    "/Users/apple/local/petsc/src/ksp/f90-mod",                                                      
    "/Users/apple/local/petsc/src/vec/f90-mod",                                                      
    "/Users/apple/local/petsc/src/snes/f90-mod"                                                      
  ],                                                                                                
  "ext_source_dirs": ["/Users/apple/local/petsc/src/snes/f90-mod/",                                  
    "/Users/apple/local/petsc/src/sys/f90-mod/",                                                     
    "/Users/apple/local/petsc/osx-gfc/include/**",                                                   
    "/Users/apple/local/petsc/src/vec/f90-mod",                                                      
    "/Users/apple/local/petsc/src/dm/f90-mod",                                                       
    "/Users/apple/local/petsc/src/mat/f90-mod",                                                      
    "/Users/apple/local/petsc/src/ksp/f90-mod",                                                      
    "/Users/apple/local/petsc/src/vec/f90-mod"                                                       
  ],                                                                                                
  "hover_language": "fortran90",                                                                    
  "hover_signature": true,                                                                          
  "use_signature_help": true  
}

But the type keyword like SNES and KSP are not recognized by the IDE at all.


Solution

  • You need to slightly tweak your language server settings. You are attempting to use the PETSc preprocessor variables without "defining" them in the language server. Parsing files containing preprocessor macros can sometimes be a bit flimsy in the language server.

    Assuming you are using fortls you can define preprocessor definitions as shown in the documentation, see: https://gnikit.github.io/fortls/options.html#pp-defs

    You should also be able to do that via the VS Code settings.

    Beware of this bug (https://github.com/gnikit/fortls/issues/72) where defining a preprocessor macro will override actual fortran tokens like .IF or ELSE


    Full disclosure I am the author of fortls and the Modern Fortran vscode extension