Skip to content

Commit b7f6b24

Browse files
committed
Add support for a default path/package.
1 parent 8e39417 commit b7f6b24

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

RustEnhanced.sublime-commands

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
"caption": "Rust: Set Cargo Features",
3636
"command": "cargo_set_features"
3737
},
38+
{
39+
"caption": "Rust: Set Default Path",
40+
"command": "cargo_set_default_path"
41+
},
3842
{
3943
"caption": "Rust: Create New Cargo Build Variant",
4044
"command": "cargo_create_new_build"

cargo_build.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,22 @@ def _determine_working_path(self, on_done):
6363
self.working_dir = working_dir
6464
self.settings_path = working_dir
6565
return on_done()
66+
6667
script_path = self.initial_settings.get('script_path')
6768
if script_path:
6869
self.working_dir = os.path.dirname(script_path)
6970
self.settings_path = script_path
7071
return on_done()
7172

73+
default_path = self.settings.get('default_path')
74+
if default_path:
75+
self.settings_path = default_path
76+
if os.path.isfile(default_path):
77+
self.working_dir = os.path.dirname(default_path)
78+
else:
79+
self.working_dir = default_path
80+
return on_done()
81+
7282
if self.command_info.get('requires_manifest', True):
7383
cmd = CargoConfigPackage(self.window)
7484
cmd.run(functools.partial(self._on_manifest_choice, on_done))

docs/build.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ are:
5656

5757
Command | Description
5858
------- | -----------
59+
Rust: Set Default Path | Set the default Cargo package to build. If not specified, then it will detect it from the current view, or display a panel to choose from.
5960
Rust: Set Cargo Target | Set the Cargo target (`--lib`, `--example foo`, etc.) for each build variant. The "Automatic Detection" option will attempt to determine which target to use based on the current active view in Sublime (a test file will use `--test` or a binary will use `--bin`, etc.).
6061
Rust: Set Cargo Build Profile | Set whether or not to use the `--release` flag.
6162
Rust: Set Cargo Target Triple | Set the target triple (such as `x86_64-apple-darwin`).
@@ -89,7 +90,7 @@ An example of a `sublime-project` file:
8990
"settings": {
9091
"cargo_build": {
9192
"paths": {
92-
"path/to/package": {
93+
"/path/to/package": {
9394
"defaults": {
9495
"release": true
9596
},
@@ -117,7 +118,7 @@ The available settings are:
117118

118119
Setting Name | Description
119120
------------ | -----------
120-
`working_dir` | The directory where to run Cargo. If not specified, attempts to detect from the active view, or displays a panel to choose a Cargo package.
121+
`working_dir` | The directory where to run Cargo. If not specified, uses the value from `default_path`, otherwise attempts to detect from the active view, or displays a panel to choose a Cargo package.
121122
`script_path` | Path to a `.rs` script, used by `cargo script` if you want to hard-code a specific script to run.
122123
`release` | If true, uses the `--release` flag.
123124
`target_triple` | If set, uses the `--target` flag with the given value.

rust/cargo_config.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class CargoConfigBase(sublime_plugin.WindowCommand):
6262
# If True, the command wants the 'package' choice to fetch metadata from
6363
# Cargo.
6464
package_wants_metadata = True
65+
# If True, the 'package' choice will automatically use the manifest
66+
# from the active view if it is available.
67+
package_allows_active_view_shortcut = True
6568
# This is a dictionary populated by the `items_package` method.
6669
# Key is the path to a package, the value is the metadata from Cargo.
6770
# This is used by other questions (like `items_target`) to get more
@@ -153,16 +156,18 @@ def wrapper(index):
153156
raise ValueError(item_info)
154157

155158
def items_package(self):
156-
# If there is a manifest under the current view, use that by default.
157159
view = self.window.active_view()
158-
if view.file_name():
160+
if self.package_allows_active_view_shortcut and view.file_name():
161+
# If there is a manifest under the current view, use that by
162+
# default.
159163
manifest_dir = util.find_cargo_manifest(view.file_name())
160164
if manifest_dir:
161165
if self.package_wants_metadata:
162166
metadata = get_cargo_metadata(self.window, manifest_dir)
163167
if metadata:
164168
for package in metadata['packages']:
165-
package_dir = os.path.dirname(package['manifest_path'])
169+
package_dir = os.path.dirname(
170+
package['manifest_path'])
166171
if package_dir == manifest_dir:
167172
self.packages = {
168173
manifest_dir: package
@@ -493,6 +498,24 @@ def done(self):
493498
self.choices['features'])
494499

495500

501+
class CargoSetDefaultPath(CargoConfigBase):
502+
503+
sequence = ['package']
504+
package_allows_active_view_shortcut = False
505+
506+
def items_package(self):
507+
result = super(CargoSetDefaultPath, self).items_package()
508+
items = result['items']
509+
items.insert(0, (['No Default',
510+
'Build will attempt to detect from the current view, or pop up a selection panel.'],
511+
None))
512+
result['default'] = self.settings.get('default_path')
513+
return result
514+
515+
def done(self):
516+
self.settings.set('default_path', self.choices['package'])
517+
518+
496519
class CargoCreateNewBuild(CargoConfigBase):
497520

498521
"""Command to create a new build variant, stored in the user's

rust/cargo_settings.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ def load(self):
129129
Rust Enhanced Warning: This window does not have an associated sublime-project file.
130130
Any changes to the Cargo build settings will be lost if you close the window."""))
131131

132+
def get(self, key, default=None):
133+
return self.project_data.get('settings', {})\
134+
.get('cargo_build', {})\
135+
.get(key, default)
136+
137+
def set(self, key, value):
138+
self.project_data.setdefault('settings', {})\
139+
.setdefault('cargo_build', {})[key] = value
140+
self.window.set_project_data(self.project_data)
141+
132142
def get_with_target(self, path, target, key, default=None):
133143
path = os.path.normpath(path)
134144
pdata = self.project_data.get('settings', {})\

0 commit comments

Comments
 (0)