lint: AttributeManager

This commit is contained in:
John McLear 2020-12-15 15:42:10 +00:00
parent 873987f989
commit 0897a28e70

View file

@ -1,3 +1,5 @@
'use strict';
const Changeset = require('./Changeset'); const Changeset = require('./Changeset');
const ChangesetUtils = require('./ChangesetUtils'); const ChangesetUtils = require('./ChangesetUtils');
const _ = require('./underscore'); const _ = require('./underscore');
@ -72,10 +74,12 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
const rowChangeset = this._setAttributesOnRangeByLine(row, startCol, endCol, attribs); const rowChangeset = this._setAttributesOnRangeByLine(row, startCol, endCol, attribs);
// compose changesets of all rows into a single changeset, as the range might not be continuous // compose changesets of all rows into a single changeset
// as the range might not be continuous
// due to the presence of line markers on the rows // due to the presence of line markers on the rows
if (allChangesets) { if (allChangesets) {
allChangesets = Changeset.compose(allChangesets.toString(), rowChangeset.toString(), this.rep.apool); allChangesets = Changeset.compose(
allChangesets.toString(), rowChangeset.toString(), this.rep.apool);
} else { } else {
allChangesets = rowChangeset; allChangesets = rowChangeset;
} }
@ -118,7 +122,8 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
_setAttributesOnRangeByLine(row, startCol, endCol, attribs) { _setAttributesOnRangeByLine(row, startCol, endCol, attribs) {
const builder = Changeset.builder(this.rep.lines.totalWidth()); const builder = Changeset.builder(this.rep.lines.totalWidth());
ChangesetUtils.buildKeepToStartOfRange(this.rep, builder, [row, startCol]); ChangesetUtils.buildKeepToStartOfRange(this.rep, builder, [row, startCol]);
ChangesetUtils.buildKeepRange(this.rep, builder, [row, startCol], [row, endCol], attribs, this.rep.apool); ChangesetUtils.buildKeepRange(
this.rep, builder, [row, startCol], [row, endCol], attribs, this.rep.apool);
return builder; return builder;
}, },
@ -127,9 +132,8 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
@param lineNum: the number of the line @param lineNum: the number of the line
*/ */
lineHasMarker(lineNum) { lineHasMarker(lineNum) {
const that = this; return lineAttributes.find(
(attribute) => this.getAttributeOnLine(lineNum, attribute) !== '') !== undefined;
return _.find(lineAttributes, (attribute) => that.getAttributeOnLine(lineNum, attribute) != '') !== undefined;
}, },
/* /*
@ -184,7 +188,7 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
if (!(rep.selStart && rep.selEnd)) return; if (!(rep.selStart && rep.selEnd)) return;
// If we're looking for the caret attribute not the selection // If we're looking for the caret attribute not the selection
// has the user already got a selection or is this purely a caret location? // has the user already got a selection or is this purely a caret location?
const isNotSelection = (rep.selStart[0] == rep.selEnd[0] && rep.selEnd[1] === rep.selStart[1]); const isNotSelection = (rep.selStart[0] === rep.selEnd[0] && rep.selEnd[1] === rep.selStart[1]);
if (isNotSelection) { if (isNotSelection) {
if (prevChar) { if (prevChar) {
// If it's not the start of the line // If it's not the start of the line
@ -198,21 +202,18 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
[attributeName, 'true'], [attributeName, 'true'],
], rep.apool); ], rep.apool);
const withItRegex = new RegExp(`${withIt.replace(/\*/g, '\\*')}(\\*|$)`); const withItRegex = new RegExp(`${withIt.replace(/\*/g, '\\*')}(\\*|$)`);
function hasIt(attribs) { const hasIt = (attribs) => withItRegex.test(attribs);
return withItRegex.test(attribs);
}
return rangeHasAttrib(rep.selStart, rep.selEnd); const rangeHasAttrib = (selStart, selEnd) => {
function rangeHasAttrib(selStart, selEnd) {
// if range is collapsed -> no attribs in range // if range is collapsed -> no attribs in range
if (selStart[1] == selEnd[1] && selStart[0] == selEnd[0]) return false; if (selStart[1] === selEnd[1] && selStart[0] === selEnd[0]) return false;
if (selStart[0] != selEnd[0]) { // -> More than one line selected if (selStart[0] !== selEnd[0]) { // -> More than one line selected
var hasAttrib = true; let hasAttrib = true;
// from selStart to the end of the first line // from selStart to the end of the first line
hasAttrib = hasAttrib && rangeHasAttrib(selStart, [selStart[0], rep.lines.atIndex(selStart[0]).text.length]); hasAttrib = hasAttrib && rangeHasAttrib(
selStart, [selStart[0], rep.lines.atIndex(selStart[0]).text.length]);
// for all lines in between // for all lines in between
for (let n = selStart[0] + 1; n < selEnd[0]; n++) { for (let n = selStart[0] + 1; n < selEnd[0]; n++) {
@ -230,7 +231,7 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
const lineNum = selStart[0]; const lineNum = selStart[0];
const start = selStart[1]; const start = selStart[1];
const end = selEnd[1]; const end = selEnd[1];
var hasAttrib = true; let hasAttrib = true;
// Iterate over attribs on this line // Iterate over attribs on this line
@ -244,7 +245,8 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
if (!hasIt(op.attribs)) { if (!hasIt(op.attribs)) {
// does op overlap selection? // does op overlap selection?
if (!(opEndInLine <= start || opStartInLine >= end)) { if (!(opEndInLine <= start || opStartInLine >= end)) {
hasAttrib = false; // since it's overlapping but hasn't got the attrib -> range hasn't got it // since it's overlapping but hasn't got the attrib -> range hasn't got it
hasAttrib = false;
break; break;
} }
} }
@ -252,7 +254,8 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
} }
return hasAttrib; return hasAttrib;
} };
return rangeHasAttrib(rep.selStart, rep.selEnd);
}, },
/* /*
@ -349,7 +352,7 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
const hasMarker = this.lineHasMarker(lineNum); const hasMarker = this.lineHasMarker(lineNum);
let found = false; let found = false;
const attribs = _(this.getAttributesOnLine(lineNum)).map((attrib) => { const attribs = this.getAttributesOnLine(lineNum).map((attrib) => {
if (attrib[0] === attributeName && (!attributeValue || attrib[0] === attributeValue)) { if (attrib[0] === attributeName && (!attributeValue || attrib[0] === attributeValue)) {
found = true; found = true;
return [attrib[0], '']; return [attrib[0], ''];
@ -373,7 +376,8 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
if (hasMarker && !countAttribsWithMarker) { if (hasMarker && !countAttribsWithMarker) {
ChangesetUtils.buildRemoveRange(this.rep, builder, [lineNum, 0], [lineNum, 1]); ChangesetUtils.buildRemoveRange(this.rep, builder, [lineNum, 0], [lineNum, 1]);
} else { } else {
ChangesetUtils.buildKeepRange(this.rep, builder, [lineNum, 0], [lineNum, 1], attribs, this.rep.apool); ChangesetUtils.buildKeepRange(
this.rep, builder, [lineNum, 0], [lineNum, 1], attribs, this.rep.apool);
} }
return this.applyChangeset(builder); return this.applyChangeset(builder);
@ -394,7 +398,9 @@ AttributeManager.prototype = _(AttributeManager.prototype).extend({
}, },
hasAttributeOnSelectionOrCaretPosition(attributeName) { hasAttributeOnSelectionOrCaretPosition(attributeName) {
const hasSelection = ((this.rep.selStart[0] !== this.rep.selEnd[0]) || (this.rep.selEnd[1] !== this.rep.selStart[1])); const hasSelection = (
(this.rep.selStart[0] !== this.rep.selEnd[0]) || (this.rep.selEnd[1] !== this.rep.selStart[1])
);
let hasAttrib; let hasAttrib;
if (hasSelection) { if (hasSelection) {
hasAttrib = this.getAttributeOnSelection(attributeName); hasAttrib = this.getAttributeOnSelection(attributeName);