Bug 1838857 - [devtools] Use devtools.editor.tabsize value for space indentation in prettifyCSS. r=devtools-reviewers,ochameau.

We used to only indent with one space when `devtools.editor.expandtab` was true.
Unit test is updated to make sure we do use the pref value now.

Differential Revision: https://phabricator.services.mozilla.com/D181225
This commit is contained in:
Nicolas Chevobbe 2023-06-20 07:42:20 +00:00
parent 3923f9374c
commit 70d26c94ef
3 changed files with 54 additions and 25 deletions

View file

@ -22,7 +22,7 @@ const DETECT_INDENT_MAX_LINES = 500;
*/ */
function getTabPrefs() { function getTabPrefs() {
const indentWithTabs = !Services.prefs.getBoolPref(EXPAND_TAB); const indentWithTabs = !Services.prefs.getBoolPref(EXPAND_TAB);
const indentUnit = Services.prefs.getIntPref(TAB_SIZE); const indentUnit = Services.prefs.getIntPref(TAB_SIZE, 2);
return { indentUnit, indentWithTabs }; return { indentUnit, indentWithTabs };
} }

View file

@ -412,6 +412,13 @@ function prettifyCSS(text, ruleCount) {
return token; return token;
}; };
// Get preference of the user regarding what to use for indentation,
// spaces or tabs.
const tabPrefs = getTabPrefs();
const baseIndentString = tabPrefs.indentWithTabs
? TAB_CHARS
: SPACE_CHARS.repeat(tabPrefs.indentUnit);
while (true) { while (true) {
// Set the initial state. // Set the initial state.
startIndex = undefined; startIndex = undefined;
@ -437,20 +444,16 @@ function prettifyCSS(text, ruleCount) {
} }
} }
// Get preference of the user regarding what to use for indentation,
// spaces or tabs.
const tabPrefs = getTabPrefs();
if (isCloseBrace) { if (isCloseBrace) {
// Even if the stylesheet contains extra closing braces, the indent level should // Even if the stylesheet contains extra closing braces, the indent level should
// remain > 0. // remain > 0.
indentLevel = Math.max(0, indentLevel - 1); indentLevel = Math.max(0, indentLevel - 1);
indent = baseIndentString.repeat(indentLevel);
// FIXME: This is incorrect and should be fixed in Bug 1839297
if (tabPrefs.indentWithTabs) { if (tabPrefs.indentWithTabs) {
indent = TAB_CHARS.repeat(indentLevel);
indentOffset = 4 * indentLevel; indentOffset = 4 * indentLevel;
} else { } else {
indent = SPACE_CHARS.repeat(indentLevel);
indentOffset = 1 * indentLevel; indentOffset = 1 * indentLevel;
} }
result = result + indent + "}"; result = result + indent + "}";
@ -466,11 +469,14 @@ function prettifyCSS(text, ruleCount) {
columnOffset++; columnOffset++;
} }
result += "{"; result += "{";
indentLevel++;
indent = baseIndentString.repeat(indentLevel);
indentOffset = indent.length;
// FIXME: This is incorrect and should be fixed in Bug 1839297
if (tabPrefs.indentWithTabs) { if (tabPrefs.indentWithTabs) {
indent = TAB_CHARS.repeat(++indentLevel);
indentOffset = 4 * indentLevel; indentOffset = 4 * indentLevel;
} else { } else {
indent = SPACE_CHARS.repeat(++indentLevel);
indentOffset = 1 * indentLevel; indentOffset = 1 * indentLevel;
} }
} }

View file

@ -10,6 +10,7 @@ const {
} = require("resource://devtools/shared/inspector/css-logic.js"); } = require("resource://devtools/shared/inspector/css-logic.js");
const EXPAND_TAB = "devtools.editor.expandtab"; const EXPAND_TAB = "devtools.editor.expandtab";
const TAB_SIZE = "devtools.editor.tabsize";
const TESTS_TAB_INDENT = [ const TESTS_TAB_INDENT = [
{ {
@ -59,7 +60,11 @@ const TESTS_SPACE_INDENT = [
{ {
name: "simple test. indent using spaces", name: "simple test. indent using spaces",
input: "div { font-family:'Arial Black', Arial, sans-serif; }", input: "div { font-family:'Arial Black', Arial, sans-serif; }",
expected: ["div {", " font-family:'Arial Black', Arial, sans-serif;", "}"], expected: [
"div {",
" font-family:'Arial Black', Arial, sans-serif;",
"}",
],
}, },
{ {
@ -95,7 +100,15 @@ const TESTS_SPACE_INDENT = [
{ {
name: "CSS with extra closing brace. indent using spaces", name: "CSS with extra closing brace. indent using spaces",
input: "body{margin:0}} div{color:red}", input: "body{margin:0}} div{color:red}",
expected: ["body {", " margin:0", "}", "}", "div {", " color:red", "}"], expected: [
"body {",
" margin:0",
"}",
"}",
"div {",
" color:red",
"}",
],
}, },
{ {
@ -136,7 +149,14 @@ const TESTS_SPACE_INDENT = [
{ {
name: "Multiline comment in CSS", name: "Multiline comment in CSS",
input: "/*\n * comment\n */\n#example{display:grid;}", input: "/*\n * comment\n */\n#example{display:grid;}",
expected: ["/*", " * comment", " */", "#example {", " display:grid;", "}"], expected: [
"/*",
" * comment",
" */",
"#example {",
" display:grid;",
"}",
],
}, },
]; ];
@ -146,6 +166,8 @@ function run_test() {
prettifyCSS(""); prettifyCSS("");
Services.prefs.setBoolPref(EXPAND_TAB, true); Services.prefs.setBoolPref(EXPAND_TAB, true);
Services.prefs.setIntPref(TAB_SIZE, 4);
for (const test of TESTS_SPACE_INDENT) { for (const test of TESTS_SPACE_INDENT) {
info(test.name); info(test.name);
@ -169,4 +191,5 @@ function run_test() {
equal(output, expected, test.name); equal(output, expected, test.name);
} }
Services.prefs.clearUserPref(EXPAND_TAB); Services.prefs.clearUserPref(EXPAND_TAB);
Services.prefs.clearUserPref(TAB_SIZE);
} }