From c1bd62ae241e0992be596a90f4aab9e27d44457b Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Tue, 17 Jun 2025 12:09:33 -0400 Subject: [PATCH] [mv3] Start to implement better DNR rule assistant --- platform/mv3/extension/js/develop.js | 47 ++++++++++++---------- platform/mv3/extension/js/rw-dnr-editor.js | 20 +++------ 2 files changed, 31 insertions(+), 36 deletions(-) diff --git a/platform/mv3/extension/js/develop.js b/platform/mv3/extension/js/develop.js index 517647d1b..5068d3831 100644 --- a/platform/mv3/extension/js/develop.js +++ b/platform/mv3/extension/js/develop.js @@ -375,9 +375,10 @@ class Editor { const text = lineFrom.text.trim(); if ( text.startsWith('#') ) { return out; } const path = []; - const pos = text.indexOf(':'); - if ( pos !== -1 ) { - path.push(text.slice(0, pos+1)); + const end = text.indexOf(':'); + if ( end !== -1 ) { + const beg = text.startsWith('- ') ? 2 : 0; + path.push(text.slice(beg, end+1)); } let lineNo = lineFrom.number; while ( depth > 0 && lineNo > 1 ) { @@ -432,38 +433,42 @@ class Editor { smartArrayItem(doc, from) { const line = doc.lineAt(from); + if ( line.from === 0 ) { return; } const blanks = /^ *$/.exec(line.text); if ( blanks === null ) { return; } - const lineNumberBefore = line.number - 1; + if ( this.editor.newlineAssistant ) { + const { scope } = this.getScopeAt(line.from-1, doc); + const insert = this.editor.newlineAssistant[scope]; + if ( insert ) { + this.view.dispatch({ + changes: { from: line.from, to: line.to, insert }, + selection: { anchor: line.from + insert.length }, + }); + return true; + } + } let targetIndent; if ( this.lineIsArrayItem(doc, line.number-1) ) { targetIndent = doc.line(line.number-1).text.indexOf('- '); } else if ( this.lineIsArrayItem(doc, line.number+1) ) { targetIndent = doc.line(line.number+1).text.indexOf('- '); - } else if ( this.editor.sequenceScopes && lineNumberBefore !== 0 ) { - const lineBefore = doc.line(lineNumberBefore); - const { scope, depth } = this.getScopeAt(lineBefore.to, doc); - if ( this.editor.sequenceScopes.includes(scope) ) { - targetIndent = depth * 2; - } } if ( targetIndent === undefined ) { return; } const indent = targetIndent - blanks[0].length; if ( indent < 0 || indent > 2 ) { return; } - return `${' '.repeat(indent)}- `; + const insert = `${' '.repeat(indent)}- `; + this.view.dispatch({ + changes: { from, insert }, + selection: { anchor: from + insert.length }, + }); + return true; } smartReturn(transaction) { const { from, to } = this.rangeFromTransaction(transaction); if ( from === undefined || to === undefined ) { return; } const { newDoc } = transaction; - const insert = this.smartArrayItem(newDoc, to); - if ( Boolean(insert) === false ) { return; } - this.view.dispatch({ - changes: { from: to, insert }, - selection: { anchor: to + insert.length }, - }); - return true; + return this.smartArrayItem(newDoc, to); } smartSpacebar(transaction) { @@ -475,10 +480,10 @@ class Editor { const localTo = to - line.from; const before = line.text.slice(0, localTo); if ( /^(?: {1}| {3})$/.test(before) === false ) { return; } - const insert = this.smartArrayItem(newDoc, to) || ' '; + if ( this.smartArrayItem(newDoc, to) ) { return true; } this.view.dispatch({ - changes: { from: to, insert }, - selection: { anchor: to + insert.length }, + changes: { from: to, insert: ' ' }, + selection: { anchor: to + 1 }, }); return true; } diff --git a/platform/mv3/extension/js/rw-dnr-editor.js b/platform/mv3/extension/js/rw-dnr-editor.js index a1e317e02..30e90f6b5 100644 --- a/platform/mv3/extension/js/rw-dnr-editor.js +++ b/platform/mv3/extension/js/rw-dnr-editor.js @@ -95,7 +95,7 @@ export class ReadWriteDNREditor extends DNREditor { return { before: /^$/, candidates: [ - { token: 'action:', after: '\n ' }, + { token: 'action:', after: '\n' }, { token: 'condition:', after: '\n ' }, { token: 'priority:', after: ' ' }, { token: '---', after: '\n' }, @@ -382,20 +382,10 @@ export class ReadWriteDNREditor extends DNREditor { return true; } - sequenceScopes = [ - 'action:redirect:transform:queryTransform:addOrReplaceParams:', - 'action:redirect:transform:queryTransform:removeParams:', - 'condition:resourceTypes:', - 'condition:excludedResourceTypes:', - 'condition:initiatorDomains:', - 'condition:excludedInitiatorDomains:', - 'condition:requestDomains:', - 'condition:excludedRequestDomains:', - 'condition:requestMethods:', - 'condition:excludedRequestMethods:', - 'condition:responseHeaders:', - 'condition:excludedResponseHeaders:', - ]; + newlineAssistant = { + 'action:': ' type: ', + 'action:responseHeaders:header:': ' operation: ', + }; ioAccept = '.json,application/json'; };