Simple setting in Windows

Welcome to Fortran-lang @vicent. Most of the things you are mentioning in terms of a code editor are very doable within VSCode.
Install Modern Fortran (the pre-release version). This should give you access to syntax highlighting, linting, debugging, formatting and snippets. For more advanced tasks like hover signatures, autocompletions, peeking definitions, etc. you will be prompted to install fortls our language server.

fortls - Language Server settings

The installation process for fortls on Windows currently is a bit harder than it is for Linux/MacOS but in summary you:

  • need Python >= 3.8
  • and if the location where pip installs packages is not in your PATH then you will have to explicitly set the location of the fortls.exe in VSCode like so (your path will be different):
{
  "fortran.fortls.path": "C:\\Users\\gn\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\Scripts\\fortls.exe"
}

The default settings works great for most people, but if you want to tinker have a look at the docs
https://fortls.fortran-lang.org/options.html

Linting

Linting is also available, it uses a compiler by default gfortran to check if source files contain valid syntax. Linting diagnostic results get triggered when you save a file.
There is a host of options available for linting that you can use to setup your project but in summary, you should point the linter to include all your sources and all their compiled artefacts (i.e. .mod files).
You can search through the other available options for linting under the linting section in the Modern Fortran settings withing VS Code.

Configuration and Compilation

Configuring and compiling your projects, especially when you use third party libraries can be accomplished CMake; VS Code has a very good set of extensions for easy integration CMake Tools. That being said, how you compile your code is a developer choice and not related to the editor

If you don’t want use something like CMake, for small projects you can get away with using VS Code Tasks and compiling all the sources in a single command (be careful to do it in the correct order!)

These are essentially automatic scripts that you can use to invoke the compiler directly with your chosen source files

This is a simple task that compiles the Fortran source, currently in focus in VS Code.

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile Fortran",
            "type": "shell",
            "command": "gfortran.exe",
            "args": [
                "-g",
                "-Wall",
                "${file}",
                "-o${fileDirname}/${fileBasenameNoExtension}.o"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": []
        }
    ]
}

(I think this is what pretty much CodeRunner the VS Code extension does, although I never had the need to use it.)

Using tasks, you can also make use of our own fpm - Fortran Package Manager which will compile your code and can easily fetch dependencies.

Debugging

On Windows debugging within VS Code only works with gfortran and GDB (no Intel compilers).
Since you will be using gfortran that should not matter.
Like all other debugging in VS Code you need to define a configuration in the launch.json file.

The one posted below uses the previous task (Compile Fortran) to first compile the source code and then launch it in Debug mode.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.o",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Compile Fortran"
        }
    ]
}

There is an interactive element to the Debug console, but it is limited to what GDB and all other C/C++/Fortran debuggers I know off permit, so you probably won’t be able to compute function values on the fly like in Python or R whilst paused at a breaking point.

Summary

You can use Modern Fortran for VS Code on Windows as long as you use gfortran. However, you will need to make a decision on how to configure and compile your project (like in C and C++). CMake is a great choice but can be slightly intimidating at first glance, fpm is substantially easier to use but it is also less feature-rich.

fpm can come close to this but in general C, C++ and Fortran don’t work this way.

2 Likes