Skip to content

Commit

Permalink
Add global Cargo settings for all packages. (rust-lang#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss authored and urschrei committed Jan 30, 2018
1 parent c429d9a commit a7a46b2
Show file tree
Hide file tree
Showing 9 changed files with 555 additions and 260 deletions.
4 changes: 2 additions & 2 deletions SyntaxCheckPlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def get_rustc_messages(self):
if method == 'clippy':
# Clippy does not support cargo target filters, must be run for
# all targets.
cmd = settings.get_command(command_info, self.cwd)
cmd = settings.get_command(method, command_info, self.cwd)
p = rust_proc.RustProc()
p.run(self.window, cmd['command'], self.cwd, self, env=cmd['env'])
p.wait()
Expand All @@ -134,7 +134,7 @@ def get_rustc_messages(self):
td = target_detect.TargetDetector(self.window)
targets = td.determine_targets(self.triggered_file_name)
for (target_src, target_args) in targets:
cmd = settings.get_command(command_info, self.cwd,
cmd = settings.get_command(method, command_info, self.cwd,
initial_settings={'target': ' '.join(target_args)})
if method == 'no-trans':
cmd['command'].extend(['--', '-Zno-trans', '-Zunstable-options'])
Expand Down
26 changes: 15 additions & 11 deletions cargo_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ class CargoExecCommand(sublime_plugin.WindowCommand):
This takes the following arguments:
- `command`: The command to run. Commands are defined in the
- `command`: The command name to run. Commands are defined in the
`cargo_settings` module. You can define your own custom command by
passing in `command_info`.
- `command_info`: Dictionary of values the defines how the cargo command
is constructed. See `command_settings.CARGO_COMMANDS`.
is constructed. See `cargo_settings.CARGO_COMMANDS`.
- `settings`: Dictionary of settings overriding anything set in the
Sublime project settings (see `command_settings` module).
Sublime project settings (see `cargo_settings` module).
"""

# The combined command info from `command_settings` and whatever the user
# The combined command info from `cargo_settings` and whatever the user
# passed in.
command_info = None
# Dictionary of initial settings passed in by the user.
Expand All @@ -49,7 +49,7 @@ def run(self, command=None, command_info=None, settings=None):
if command == 'auto':
self._detect_auto_build()
else:
self.command = command
self.command_name = command
self.command_info = cargo_settings.CARGO_COMMANDS\
.get(command, {}).copy()
if command_info:
Expand Down Expand Up @@ -117,7 +117,7 @@ def _determine_working_path(self, on_done):
self.settings_path = script_path
return on_done()

default_path = self.settings.get('default_path')
default_path = self.settings.get_project_base('default_path')
if default_path:
self.settings_path = default_path
if os.path.isfile(default_path):
Expand Down Expand Up @@ -153,7 +153,7 @@ def _run_check_for_args(self):
if self.command_info.get('wants_run_args', False) and \
not self.initial_settings.get('extra_run_args'):
self.window.show_input_panel('Enter extra args:',
LAST_EXTRA_ARGS.get(self.command, ''),
LAST_EXTRA_ARGS.get(self.command_name, ''),
self._on_extra_args, None, None)
else:
self._run()
Expand All @@ -165,7 +165,8 @@ def _on_extra_args(self, args):

def _run(self):
t = CargoExecThread(self.window, self.settings,
self.command_info, self.initial_settings,
self.command_name, self.command_info,
self.initial_settings,
self.settings_path, self.working_dir)
t.start()

Expand All @@ -175,17 +176,20 @@ class CargoExecThread(rust_thread.RustThread):
silently_interruptible = False
name = 'Cargo Exec'

def __init__(self, window, settings, command_info, initial_settings,
settings_path, working_dir):
def __init__(self, window, settings,
command_name, command_info,
initial_settings, settings_path, working_dir):
super(CargoExecThread, self).__init__(window)
self.settings = settings
self.command_name = command_name
self.command_info = command_info
self.initial_settings = initial_settings
self.settings_path = settings_path
self.working_dir = working_dir

def run(self):
cmd = self.settings.get_command(self.command_info,
cmd = self.settings.get_command(self.command_name,
self.command_info,
self.settings_path,
self.initial_settings)
if not cmd:
Expand Down
68 changes: 51 additions & 17 deletions docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,21 @@ It also supports Sublime's build settings:
| `show_errors_inline` | `true` | If true, messages are displayed in line using Sublime's phantoms. If false, messages are only displayed in the output panel. |
| `show_panel_on_build` | `true` | If true, an output panel is displayed at the bottom of the window showing the compiler output. |

## Cargo Project Settings
## Cargo Settings

You can customize how Cargo is run with settings stored in your
`sublime-project` file. Settings can be applied per-target (`--lib`,
`--example foo`, etc.), for specific variants ("Build", "Run", "Test", etc.),
or globally.
A variety of settings are available to customize how Cargo is run. These
settings can be set globally, per-Sublime project, per-Cargo package, for
specific build variants ("Build", "Run", "Test", etc.), or specific Cargo
targets (`--lib`, `--example foo`, etc.).

### Configure Command

To help you configure the Cargo build settings, run the `Rust: Configure Cargo
Build` command from Sublime's Command Palette (Ctrl-Shift-P / ⌘-Shift-P).
This will ask you a series of questions for the setting to configure. The
first choice is the setting:
Build` command from Sublime's Command Palette (Ctrl-Shift-P / ⌘-Shift-P). This
will ask you a series of questions for the setting to configure. It will
update your `.sublime-project` or `Users/RustEnhanced.sublime-settings` file
depending on which options you pick. The first question is the setting you
want to update:

Setting | Description
------- | -----------
Expand All @@ -88,21 +90,30 @@ Default Package/Path | The default package to build, useful if you have a worksp

If you have multiple Cargo packages in your workspace, it will ask for the package to configure.

A setting can be global (for all build invocations), for a specific build variant (such as "Test"), or for a specific build target (such as `--example myprog`).

Caution: If you have not created a `sublime-project` file, then any changes
you make will be lost if you close the Sublime window.

### Settings

Settings are stored in your `sublime-project` file under `"cargo_build"` in
the `"settings"` key. Settings are organized per Cargo package in the
`"paths"` object. Paths can either be directories to a Cargo package, or the
path to a Rust source file (when used with `cargo script`). The top-level
keys for each package are:
Cargo settings are stored in the `"cargo_build"` Sublime setting. This can be
either in your `sublime-project` file or in
`Users/RustEnhanced.sublime-settings`. `"cargo_build"` is an object with the
following keys:

Key | Description
--- | -----------
`"paths"` | Settings for specific Cargo packages.
`"default_path"` | The default Cargo package to build (useful for workspaces, see below).
`"variants"` | Settings per build variant.
`"defaults"` | Default settings used if not set per target or variant.

Paths should be an absolute path to the directory of a Cargo package, or the
path to a Rust source file (when used with `cargo script`).

`"paths"` is an object of path keys mapping to an object with the keys:

Path Key | Description
-------- | -----------
`"defaults"` | Default settings used if not set per target or variant.
`"targets"` | Settings per target (such as `"--lib"` or `"--bin foo"`).
`"variants"` | Settings per build variant.
Expand All @@ -119,7 +130,7 @@ An example of a `sublime-project` file:
"paths": {
"/path/to/package": {
"defaults": {
"release": true
"release": false
},
"targets": {
"--example ex1": {
Expand All @@ -136,7 +147,17 @@ An example of a `sublime-project` file:
}
}
},
"default_path": "/path/to/package"
"default_path": "/path/to/package",
"variants": {
"run": {
"env": {
"RUST_BACKTRACE": 1
}
}
},
"defaults": {
"release": true
}
}
}
}
Expand All @@ -162,6 +183,19 @@ The extra args settings support standard Sublime variable expansion (see
[Build System
Variables](http://docs.sublimetext.info/en/latest/reference/build_systems/configuration.html#build-system-variables))

### Setting Precedence

The Cargo commands will generally use the most specific setting available.
The order they are searched are (first found value wins):

1. `.sublime-project` > Cargo Package > Cargo Target
2. `.sublime-project` > Cargo Package > Build Variant
3. `.sublime-project` > Cargo Package > Defaults
4. `.sublime-project` > Build Variant
5. `RustEnhanced.sublime-settings` > Build Variant
6. `.sublime-project` > Defaults
7. `RustEnhanced.sublime-settings` > Defaults

## Multiple Cargo Projects (Advanced)

You can have multiple Cargo projects in a single Sublime project (such as when
Expand Down
Loading

0 comments on commit a7a46b2

Please sign in to comment.