Change behavior of “adjustment exceeds bounds” error

Previously, we threw an error when a scope adjustment violated its bounds constraints, but that's a bit disruptive for everyday use. Instead, we throw an error in dev mode (so that the grammar's author doesn't fail to notice the problem), but downgrade it to a warning outside of dev mode so that it's recoverable.

There's a chance that the warning will be _too_ subtle, but we'll give it a shot.

We also include more diagnostic information so that it's clearer exactly _where_ the violation is happening.
This commit is contained in:
Andrew Dupont 2023-09-11 15:14:19 -07:00
parent 878ef168c2
commit e9903ad9d6
2 changed files with 21 additions and 2 deletions

View File

@ -228,7 +228,7 @@ describe('ScopeResolver', () => {
let bar = "this is a line below a comment"
`);
// Prevent an exception from being thrown before we can even check the
// scopeResovler.
// scopeResolver.
spyOn(languageMode, 'isRowCommented').andReturn(false);
await languageMode.ready;

View File

@ -16,6 +16,11 @@ function comparePoints(a, b) {
}
}
function rangeSpecToString (range) {
let [sp, ep] = [range.startPosition, range.endPosition];
return `(${sp.row}, ${sp.column}) - (${ep.row}, ${ep.column})`;
}
function resolveNodeDescriptor(node, descriptor) {
let parts = descriptor.split('.');
let result = node;
@ -266,6 +271,20 @@ class ScopeResolver {
return ScopeResolver.CAPTURE_SETTINGS[prop](...args);
}
warnAboutExceededRange(range, capture) {
let msg = ['Cannot extend past original range of capture!'];
msg.push(`Scope name: ${capture.name}`);
msg.push(`Original range: ${rangeSpecToString(capture.node)}`);
msg.push(`Adjusted range: ${rangeSpecToString(range)}`);
if (atom.inDevMode()) {
throw new Error(msg.join('\n'));
}
console.warn(msg.join('\n'));
}
// Given a capture and possible predicate data, determines the buffer range
// that this capture wants to cover.
determineCaptureRange(capture) {
@ -299,7 +318,7 @@ class ScopeResolver {
}
if (this.rangeExceedsBoundsOfCapture(range, capture)) {
throw new Error('Cannot extend past original range of capture');
this.warnAboutExceededRange(range, capture);
}
// Any invalidity in the returned range means we shouldn't store this