Skip to content

Commit 65a5c32

Browse files
ehussjasonwilliams
authored andcommitted
Add compiler suggestions and message themes. (#267)
1 parent eb7783f commit 65a5c32

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1502
-659
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ It is possible that the built-in `Rust` package will cause conflicts with Rust E
3030
## Features
3131
### Go To Definition
3232
### Cargo Build
33-
Rust Enhanced has a custom build system tailored for running Cargo. It will display errors and warnings in line using Sublime's phantoms. It also supports a variety of configuration options to control how Cargo is run.
33+
Rust Enhanced has a custom build system tailored for running Cargo. It will display errors and warnings in line using Sublime's phantoms (see [Messages](docs/messages.md) for settings to control how messages are displayed). It also supports a variety of configuration options to control how Cargo is run.
3434

3535
![testingrust](https://cloud.githubusercontent.com/assets/43198/22944409/7780ab9a-f2a5-11e6-87ea-0e253d6c40f6.png)
3636

@@ -119,7 +119,7 @@ To customize the settings, use the command from the Sublime menu:
119119
Additionally, you can customize settings per-project by adding settings to your `.sublime-project` file under the `"settings"` key.
120120

121121
## Development
122-
Development is quite simple, just check out this project to your Sublime Text 3 packages folder, and switch to using this one.
122+
Development is quite simple, just check out this project to your Sublime Text 3 packages folder, and switch to using this one.
123123
Syntax definitions are defined in the `RustEnhanced.sublime-syntax` file.
124124

125125
## Credits

RustEnhanced.sublime-settings

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// If true, will not display warning messages.
1515
"rust_syntax_hide_warnings": false,
1616

17-
// Color of messages.
17+
// Color of messages for "clear" theme.
1818
// These use CSS colors. See
1919
// https://www.sublimetext.com/docs/3/minihtml.html for more detail.
2020
"rust_syntax_error_color": "var(--redish)",
@@ -46,6 +46,11 @@
4646
// "none" - Do not place icons in the gutter.
4747
"rust_gutter_style": "shape",
4848

49+
// Style for displaying inline messages. Can be:
50+
// "clear" - Clear background with colors matching your color scheme.
51+
// "solid" - Solid background color.
52+
"rust_message_theme": "clear",
53+
4954
// If your cargo project has several build targets, it's possible to specify mapping of
5055
// source code filenames to the target names to enable syntax checking.
5156
// "projects": {

cargo_build.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import functools
44
import sublime
55
import sublime_plugin
6+
import sys
67
from .rust import (rust_proc, rust_thread, opanel, util, messages,
78
cargo_settings, target_detect)
89
from .rust.cargo_config import *
@@ -486,3 +487,34 @@ def is_applicable(cls, settings):
486487
def on_hover(self, point, hover_zone):
487488
if util.get_setting('rust_phantom_style', 'normal') == 'popup':
488489
messages.message_popup(self.view, point, hover_zone)
490+
491+
492+
class RustAcceptSuggestedReplacement(sublime_plugin.TextCommand):
493+
494+
"""Used for suggested replacements issued by the compiler to apply the
495+
suggested replacement.
496+
"""
497+
498+
def run(self, edit, region, replacement):
499+
region = sublime.Region(*region)
500+
self.view.replace(edit, region, replacement)
501+
502+
503+
def plugin_unloaded():
504+
messages.clear_all_messages()
505+
try:
506+
from package_control import events
507+
except ImportError:
508+
return
509+
package_name = __package__.split('.')[0]
510+
if events.pre_upgrade(package_name):
511+
# When upgrading the package, Sublime currently does not cleanly
512+
# unload the `rust` Python package. This is a workaround to ensure
513+
# that it gets completely unloaded so that when it upgrades it will
514+
# load the new package. See
515+
# https://github.com/SublimeTextIssues/Core/issues/2207
516+
re_keys = [key for key in sys.modules if key.startswith(package_name + '.rust')]
517+
for key in re_keys:
518+
del sys.modules[key]
519+
if package_name in sys.modules:
520+
del sys.modules[package_name]

changelog/2.11.0.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Rust Enhanced 2.11.0
2+
3+
You must restart Sublime after installing this update.
4+
5+
## New Features
6+
- Added `"rust_message_theme"` configuration setting for choosing different
7+
styles of inline messages. Currently two options are available: "clear" and
8+
"solid". See
9+
https://github.com/rust-lang/rust-enhanced/blob/master/docs/build.md#general-settings
10+
for examples.
11+
12+
- If the Rust compiler provides a suggestion on how to fix an error, the
13+
inline messages now include a link that you can click to automatically apply
14+
the suggestion.
15+
16+
## Syntax Updates
17+
- Support u128/i128 integer suffix.

docs/build.md

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ The Rust Enhanced build system provides an interface for running Cargo. It can
44
show inline warning and error messages. It also has a variety of ways of
55
configuring options for how Cargo is run.
66

7+
See [Messages](messages.md) for settings to control how compiler messages are
8+
displayed.
9+
710
## Usage
811

912
When Sublime is set to use "Automatic" build system detection, it will choose
@@ -42,28 +45,6 @@ Document | <code>cargo&nbsp;doc</code> | Builds package documentation.
4245
Clippy | <code>cargo&nbsp;clippy</code> | Runs [Clippy](https://github.com/Manishearth/rust-clippy). Clippy must be installed, and currently requires the nightly toolchain.
4346
Script | <code>cargo&nbsp;script&nbsp;$path</code> | Runs [Cargo Script](https://github.com/DanielKeep/cargo-script). Cargo Script must be installed. This is an addon that allows you to run a Rust source file like a script (without a Cargo.toml manifest).
4447

45-
## General Settings
46-
47-
General settings (see [Settings](../README.md#settings)) for how messages are displayed are:
48-
49-
| Setting | Default | Description |
50-
| :------ | :------ | :---------- |
51-
| `rust_syntax_hide_warnings` | `false` | If true, will not display warning messages. |
52-
| `rust_syntax_error_color` | `"var(--redish)"` | Color of error messages. |
53-
| `rust_syntax_warning_color` | `"var(--yellowish)"` | Color of warning messages. |
54-
| `rust_syntax_note_color` | `"var(--greenish)"` | Color of note messages. |
55-
| `rust_syntax_help_color` | `"var(--bluish)"` | Color of help messages. |
56-
| `rust_phantom_style` | `"normal"` | How to display inline messages. Either `normal`, `popup`, or `none`. |
57-
| `rust_region_style` | `"outline"` | How to highlight messages. Either `outline` or `none`. |
58-
| `rust_gutter_style` | `"shape"` | Type of icon to show in the gutter. Either `shape`, `circle`, or `none`. |
59-
60-
It also supports Sublime's build settings:
61-
62-
| Setting | Default | Description |
63-
| :------ | :------ | :---------- |
64-
| `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. |
65-
| `show_panel_on_build` | `true` | If true, an output panel is displayed at the bottom of the window showing the compiler output. |
66-
6748
## Cargo Settings
6849

6950
A variety of settings are available to customize how Cargo is run. These

docs/img/messages_popup.gif

14.2 KB
Loading

docs/img/region_style_none.png

2.51 KB
Loading

docs/img/region_style_outline.png

2.56 KB
Loading

docs/img/show_errors_inline_false.png

16 KB
Loading

docs/img/show_errors_inline_true.png

11.5 KB
Loading

docs/img/theme_clear.png

13.7 KB
Loading

docs/img/theme_solid.png

16.2 KB
Loading

docs/messages.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Messages
2+
3+
There are a variety of ways to display Rust compiler messages. See
4+
[Settings](../README.md#settings) for more details about how to configure
5+
settings.
6+
7+
## Inline Phantoms vs Output Panel
8+
9+
The `show_errors_inline` setting controls whether or not errors are shown
10+
inline with the code using Sublime's "phantoms". If it is `true`, it will
11+
also display an abbreviated message in the output panel. If it is `false`,
12+
messages will only be displayed in the output panel, using rustc's formatting.
13+
14+
### `show_errors_inline`
15+
16+
<table>
17+
<tr>
18+
<td><code>true</code></td>
19+
<td><img src="img/show_errors_inline_true.png"></td>
20+
</tr>
21+
<tr>
22+
<td><code>false</code></td>
23+
<td><img src="img/show_errors_inline_false.png"></td>
24+
</tr>
25+
</table>
26+
27+
## Popup Phantom Style
28+
29+
Phantoms can be displayed inline with the code, or as a popup when the mouse
30+
hovers over an error (either the gutter icon or the error outline). The
31+
`rust_phantom_style` setting controls this behavior.
32+
33+
### `rust_phantom_style`
34+
35+
| Value | Description |
36+
| :---- | :---------- |
37+
| `normal` | Phantoms are displayed inline. |
38+
| `popup` | Phantoms are displayed when the mouse hovers over an error. |
39+
| `none` | Phantoms are not displayed. |
40+
41+
<img src="img/messages_popup.gif">
42+
43+
## Phantom Themes
44+
45+
The style of the phantom messages is controlled with the `rust_message_theme`
46+
setting. Currently the following themes are available:
47+
48+
### `rust_message_theme`
49+
50+
<table>
51+
<tr>
52+
<td><code>clear</code></td>
53+
<td><img src="img/theme_clear.png"></td>
54+
</tr>
55+
<tr>
56+
<td><code>solid</code></td>
57+
<td><img src="img/theme_solid.png"></td>
58+
</tr>
59+
</table>
60+
61+
### Clear Theme Colors
62+
63+
The `clear` theme is designed to integrate with your chosen Color Scheme. You
64+
can customize the colors of the messages with the following settings.
65+
66+
| Setting | Default | Description |
67+
| :------ | :------ | :---------- |
68+
| `rust_syntax_error_color` | `"var(--redish)"` | Color of error messages. |
69+
| `rust_syntax_warning_color` | `"var(--yellowish)"` | Color of warning messages. |
70+
| `rust_syntax_note_color` | `"var(--greenish)"` | Color of note messages. |
71+
| `rust_syntax_help_color` | `"var(--bluish)"` | Color of help messages. |
72+
73+
74+
## Region Highlighting
75+
76+
The span of code for a compiler message is by default highlighted with an
77+
outline.
78+
79+
### `rust_region_style`
80+
81+
| Value | Example | Description |
82+
| :---- | :------ | :---------- |
83+
| `outline` | <img src="img/region_style_outline.png"> | Regions are highlighted with an outline. |
84+
| `none` | <img src="img/region_style_none.png"> | Regions are not highlighted. |
85+
86+
## Gutter Images
87+
88+
The gutter (beside the line numbers) will include an icon indicating the level
89+
of the message. The styling of these icons is controlled with
90+
`rust_gutter_style`.
91+
92+
### `rust_gutter_style`
93+
94+
| Value | Description |
95+
| :---- | :---------- |
96+
| `shape` | <img src="../images/gutter/shape-error.png"> <img src="../images/gutter/shape-warning.png"> <img src="../images/gutter/shape-note.png"> <img src="../images/gutter/shape-help.png"> |
97+
| `circle` | <img src="../images/gutter/circle-error.png"> <img src="../images/gutter/circle-warning.png"> <img src="../images/gutter/circle-note.png"> <img src="../images/gutter/circle-help.png"> |
98+
| `none` | Do not display icons. |
99+
100+
## Other Settings
101+
102+
A few other settings are available for controlling messages:
103+
104+
| Setting | Default | Description |
105+
| :------ | :------ | :---------- |
106+
| `show_panel_on_build` | `true` | If true, an output panel is displayed at the bottom of the window showing the compiler output. |
107+
| `rust_syntax_hide_warnings` | `false` | If true, will not display warning messages. |

images/gutter/circle-none.png

1.91 KB
Loading

images/gutter/[email protected]

1.92 KB
Loading

images/gutter/shape-none.png

1.91 KB
Loading

images/gutter/[email protected]

1.92 KB
Loading

messages.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"2.11.0": "changelog/2.11.0.md"
3+
}

rust/batch.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
"""Classes used for aggregating messages that are on the same line."""
2+
3+
4+
class MessageBatch:
5+
6+
"""Abstract base class for a set of messages that apply to the same line.
7+
8+
:ivar children: List of additional messages, may be empty.
9+
:ivar hidden: Boolean if this message should be displayed.
10+
"""
11+
12+
hidden = False
13+
14+
def __init__(self):
15+
self.children = []
16+
17+
def __iter__(self):
18+
"""Iterates over all messages in the batch."""
19+
raise NotImplementedError()
20+
21+
def path(self):
22+
"""Returns the file path of the batch."""
23+
raise NotImplementedError()
24+
25+
def first(self):
26+
"""Returns the first message of the batch."""
27+
raise NotImplementedError()
28+
29+
def dismiss(self):
30+
"""Permanently remove this message and all its children from the
31+
view."""
32+
raise NotImplementedError()
33+
34+
def _dismiss(self, window):
35+
# There is a awkward problem with Sublime and
36+
# add_regions/erase_regions. The regions are part of the undo stack,
37+
# which means even after we erase them, they can come back from the
38+
# dead if the user hits undo. We simply mark these as "hidden" to
39+
# ensure that `clear_messages` can erase any of these zombie regions.
40+
# See https://github.com/SublimeTextIssues/Core/issues/1121
41+
# This is imperfect, since the user could do the following:
42+
# 1) Build 2) Type some text 3) Clear Messages 4) Undo
43+
# which will resurrect the regions without an easy way to remove them
44+
# (user has to close and reopen the file). I don't know of any good
45+
# workarounds.
46+
for msg in self:
47+
view = window.find_open_file(msg.path)
48+
if view:
49+
view.erase_regions(msg.region_key)
50+
view.erase_phantoms(msg.region_key)
51+
52+
53+
class PrimaryBatch(MessageBatch):
54+
55+
"""A batch of messages with the primary message.
56+
57+
:ivar primary_message: The primary message object.
58+
:ivar child_batches: List of `ChildBatch` batches associated with this
59+
batch.
60+
:ivar child_links: List of `(url, text)` tuples for links to child batches
61+
that are "far away".
62+
"""
63+
64+
primary_message = None
65+
66+
def __init__(self, primary_message):
67+
super(PrimaryBatch, self).__init__()
68+
self.primary_message = primary_message
69+
self.child_batches = []
70+
self.child_links = []
71+
72+
def __iter__(self):
73+
yield self.primary_message
74+
for child in self.children:
75+
yield child
76+
77+
def path(self):
78+
return self.primary_message.path
79+
80+
def first(self):
81+
return self.primary_message
82+
83+
def dismiss(self, window):
84+
self.hidden = True
85+
self._dismiss(window)
86+
for batch in self.child_batches:
87+
batch._dismiss(window)
88+
89+
90+
class ChildBatch(MessageBatch):
91+
92+
"""A batch of messages that are associated with a primary message.
93+
94+
:ivar primary_batch: The `PrimaryBatch` this is associated with.
95+
:ivar back_link: Tuple of `(url, text)` of the link to the primary batch
96+
if it is "far away" (otherwise None).
97+
"""
98+
99+
primary_batch = None
100+
back_link = None
101+
102+
def __init__(self, primary_batch):
103+
super(ChildBatch, self).__init__()
104+
self.primary_batch = primary_batch
105+
106+
def __iter__(self):
107+
for child in self.children:
108+
yield child
109+
110+
def path(self):
111+
return self.children[0].path
112+
113+
def first(self):
114+
return self.children[0]
115+
116+
def dismiss(self, window):
117+
self.hidden = True
118+
self.primary_batch.dismiss(window)

0 commit comments

Comments
 (0)