Skip to content

Commit

Permalink
pw_ide: Auto-create target setting VS Code tasks
Browse files Browse the repository at this point in the history
When you `pw ide sync`, a VS Code task will be create for each target
that sets it as the current target. Now you can run that task from the
task runner menu instead of entering the target name into a text input.

Bug: b/272567586
Change-Id: Idd6d7c31aa5a5482ca537b6101fce9ebd694c67b
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/133950
Reviewed-by: Anthony DiGirolamo <[email protected]>
Commit-Queue: Chad Norvell <[email protected]>
  • Loading branch information
chadnorvell authored and CQ Bot Account committed Apr 10, 2023
1 parent de43f23 commit b669d17
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 40 deletions.
14 changes: 14 additions & 0 deletions pw_ide/docs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,20 @@ for code editors, where default settings can be defined within ``pw_ide``,
which can be overridden by project settings, which in turn can be overridden
by individual user settings.

Visual Studio Code
^^^^^^^^^^^^^^^^^^
Running ``pw ide sync`` will automatically generate settings for Visual Studio
Code. ``pw_ide`` comes with sensible defaults for Pigweed projects, but those
can be augmented or overridden at the project level or the user level using
``pw_project_settings.json`` and ``pw_user_settings.json`` respectively. The
generated ``settings.json`` file is essentially a build artifact and shouldn't
be committed to source control.

The same pattern applies to ``tasks.json``, which provides Visual Studio Code
tasks for ``pw_ide`` commands. Access these by opening the command palette
(Ctrl/Cmd-Shift-P), selecting ``Tasks: Run Task``, then selecting the desired
task.

Selected API Reference
^^^^^^^^^^^^^^^^^^^^^^
.. automodule:: pw_ide.editors
Expand Down
8 changes: 7 additions & 1 deletion pw_ide/py/pw_ide/activate.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,15 @@ def main() -> int:
return 0

if args.exec is not None:
# Ensure that the command is always dequoted.
# When executed directly from the shell, this is already done by
# default. But in other contexts, the command may be passed more
# literally with whitespace and quotes, which won't work.
exec_cmd = args.exec.strip(" '")

# We're executing a command in a subprocess with the modified env.
return subprocess.run(
args.exec, env=modified_env.env, shell=True
exec_cmd, env=modified_env.env, shell=True
).returncode

# If we got here, we're trying to modify the current shell's env.
Expand Down
2 changes: 1 addition & 1 deletion pw_ide/py/pw_ide/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ def _process_compdbs( # pylint: disable=too-many-locals
)
reporter_lines.append(
f'Processed {len(processed_compdb_files)} to working dir at '
f'${working_dir_path}'
f'{working_dir_path}'
)

if len(reporter_lines) > 0:
Expand Down
11 changes: 5 additions & 6 deletions pw_ide/py/pw_ide/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pw_cli.yaml_config_loader_mixin import YamlConfigLoaderMixin

env = pigweed_environment()
env_vars = vars(env)

PW_IDE_DIR_NAME = '.pw_ide'
PW_IDE_DEFAULT_DIR = Path(env.PW_PROJECT_ROOT) / PW_IDE_DIR_NAME
Expand Down Expand Up @@ -251,13 +252,11 @@ def clangd_query_drivers(self) -> List[str]:
],
]

if env.PW_PIGWEED_CIPD_INSTALL_DIR is not None:
drivers.append(
str(Path(env.PW_PIGWEED_CIPD_INSTALL_DIR) / 'bin' / '*')
)
if (env_var := env_vars.get('PW_PIGWEED_CIPD_INSTALL_DIR')) is not None:
drivers.append(str(Path(env_var) / 'bin' / '*'))

if env.PW_ARM_CIPD_INSTALL_DIR is not None:
drivers.append(str(Path(env.PW_ARM_CIPD_INSTALL_DIR) / 'bin' / '*'))
if (env_var := env_vars.get('PW_ARM_CIPD_INSTALL_DIR')) is not None:
drivers.append(str(Path(env_var) / 'bin' / '*'))

return drivers

Expand Down
112 changes: 80 additions & 32 deletions pw_ide/py/pw_ide/vscode.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@
import os
from pathlib import Path
import platform
from typing import Any, Dict, List, OrderedDict
from typing import Any, Dict, List, Optional, OrderedDict

from pw_ide.activate import BashShellModifier
from pw_ide.cpp import ClangdSettings
from pw_ide.cpp import ClangdSettings, CppIdeFeaturesState

from pw_ide.editors import (
EditorSettingsDict,
Expand Down Expand Up @@ -187,21 +187,36 @@ def _local_python_settings() -> Dict[str, Any]:
}
)

# pylint: disable=line-too-long
_DEFAULT_TASKS: EditorSettingsDict = OrderedDict(
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"type": "process",
"label": "Pigweed IDE: Format",
"command": "${workspaceFolder}/.pw_ide/python ${workspaceFolder}/pw_ide/py/pw_ide/activate.py -x 'pw format --fix'",
"command": "${config:python.defaultInterpreterPath}",
"args": [
"-m",
"pw_ide.activate",
"-x 'pw format --fix'",
],
"presentation": {
"focus": True,
},
"problemMatcher": [],
},
{
"type": "shell",
"type": "process",
"label": "Pigweed IDE: Presubmit",
"command": "${workspaceFolder}/.pw_ide/python ${workspaceFolder}/pw_ide/py/pw_ide/activate.py -x 'pw presubmit'",
"command": "${config:python.defaultInterpreterPath}",
"args": [
"-m",
"pw_ide.activate",
"-x 'pw presubmit'",
],
"presentation": {
"focus": True,
},
"problemMatcher": [],
},
{
Expand All @@ -220,46 +235,64 @@ def _local_python_settings() -> Dict[str, Any]:
"problemMatcher": [],
},
{
"type": "shell",
"label": "Pigweed IDE: Process C++ Compilation Database from GN",
"command": "${workspaceFolder}/.pw_ide/python ${workspaceFolder}/pw_ide/py/pw_ide/activate.py -x 'pw ide cpp --gn --process out/compile_commands.json'",
"type": "process",
"label": "Pigweed IDE: Sync",
"command": "${config:python.defaultInterpreterPath}",
"args": [
"-m",
"pw_ide.activate",
"-x 'pw ide sync'",
],
"presentation": {
"focus": True,
},
"problemMatcher": [],
},
{
"type": "shell",
"label": "Pigweed IDE: Setup",
"command": "python3 ${workspaceFolder}/pw_ide/py/pw_ide/activate.py -x 'pw ide setup'",
"problemMatcher": [],
},
{
"type": "shell",
"type": "process",
"label": "Pigweed IDE: Current C++ Code Analysis Target",
"command": "${workspaceFolder}/.pw_ide/python ${workspaceFolder}/pw_ide/py/pw_ide/activate.py -x 'pw ide cpp'",
"command": "${config:python.defaultInterpreterPath}",
"args": [
"-m",
"pw_ide.activate",
"-x 'pw ide cpp'",
],
"presentation": {
"focus": True,
},
"problemMatcher": [],
},
{
"type": "shell",
"type": "process",
"label": "Pigweed IDE: List C++ Code Analysis Targets",
"command": "${workspaceFolder}/.pw_ide/python ${workspaceFolder}/pw_ide/py/pw_ide/activate.py -x 'pw ide cpp --list'",
"command": "${config:python.defaultInterpreterPath}",
"args": [
"-m",
"pw_ide.activate",
"-x 'pw ide cpp --list'",
],
"presentation": {
"focus": True,
},
"problemMatcher": [],
},
{
"type": "shell",
"type": "process",
"label": "Pigweed IDE: Set C++ Code Analysis Target",
"command": "${workspaceFolder}/.pw_ide/python ${workspaceFolder}/pw_ide/py/pw_ide/activate.py -x 'pw ide cpp --set ${input:target}'",
"command": "${config:python.defaultInterpreterPath}",
"args": [
"-m",
"pw_ide.activate",
"-x 'pw ide cpp --set ${input:availableTargets}'",
],
"presentation": {
"focus": True,
},
"problemMatcher": [],
},
],
"inputs": [
{
"id": "target",
"type": "promptString",
"description": "C++ code analysis target",
}
],
}
)
# pylint: enable=line-too-long

_DEFAULT_EXTENSIONS: EditorSettingsDict = OrderedDict(
{
Expand Down Expand Up @@ -295,8 +328,23 @@ def _default_settings(
)


def _default_tasks(_pw_ide_settings: PigweedIdeSettings) -> EditorSettingsDict:
return _DEFAULT_TASKS
def _default_tasks(
pw_ide_settings: PigweedIdeSettings,
state: Optional[CppIdeFeaturesState] = None,
) -> EditorSettingsDict:
if state is None:
state = CppIdeFeaturesState(pw_ide_settings)

inputs = [
{
"type": "pickString",
"id": "availableTargets",
"description": "Available targets",
"options": list(state.targets),
}
]

return OrderedDict(**_DEFAULT_TASKS, inputs=inputs)


def _default_extensions(
Expand Down

0 comments on commit b669d17

Please sign in to comment.