-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Return the css property value range in getInfoAtPos() api. #7390
Changes from 7 commits
e489b36
d82b043
045f312
241352b
0b6747d
a4465a6
14987bb
d5b8102
870e99a
99dbf8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,8 @@ define(function (require, exports, module) { | |
| EditorManager = require("editor/EditorManager"), | ||
| HTMLUtils = require("language/HTMLUtils"), | ||
| ProjectManager = require("project/ProjectManager"), | ||
| TokenUtils = require("utils/TokenUtils"); | ||
| TokenUtils = require("utils/TokenUtils"), | ||
| _ = require("thirdparty/lodash"); | ||
|
|
||
| // Constants | ||
| var SELECTOR = "selector", | ||
|
|
@@ -128,20 +129,25 @@ define(function (require, exports, module) { | |
| * @param {Array.<string>=} values An array of property values | ||
| * @param {boolean=} isNewItem If this is true, then the value in index refers to the index at which a new item | ||
| * is going to be inserted and should not be used for accessing an existing value in values array. | ||
| * @param {{start: {line: number, ch: number}, | ||
| * end: {line: number, ch: number}}=} range A range object with a start position and an end position | ||
| * @return {{context: string, | ||
| * offset: number, | ||
| * name: string, | ||
| * index: number, | ||
| * values: Array.<string>, | ||
| * isNewItem: boolean}} A CSS context info object. | ||
| * isNewItem: boolean, | ||
| * range: {start: {line: number, ch: number}, | ||
| * end: {line: number, ch: number}}}} A CSS context info object. | ||
| */ | ||
| function createInfo(context, offset, name, index, values, isNewItem) { | ||
| function createInfo(context, offset, name, index, values, isNewItem, range) { | ||
| var ruleInfo = { context: context || "", | ||
| offset: offset || 0, | ||
| name: name || "", | ||
| index: -1, | ||
| values: [], | ||
| isNewItem: (isNewItem) ? true : false }; | ||
| isNewItem: (isNewItem) ? true : false, | ||
| range: range }; | ||
|
|
||
| if (context === PROP_VALUE || context === SELECTOR || context === IMPORT_URL) { | ||
| ruleInfo.index = index; | ||
|
|
@@ -165,7 +171,9 @@ define(function (require, exports, module) { | |
| * name: string, | ||
| * index: number, | ||
| * values: Array.<string>, | ||
| * isNewItem: boolean}} A CSS context info object. | ||
| * isNewItem: boolean, | ||
| * range: {start: {line: number, ch: number}, | ||
| * end: {line: number, ch: number}}}} A CSS context info object. | ||
| */ | ||
| function _getPropNameInfo(ctx) { | ||
| var propName = "", | ||
|
|
@@ -335,6 +343,43 @@ define(function (require, exports, module) { | |
| return propValues; | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * Return a range object with a start position and an end position after | ||
| * skipping any whitespaces and all separators used before and after a | ||
| * valid property value. | ||
| * | ||
| * @param {editor:{CodeMirror}, pos:{ch:{string}, line:{number}}, token:{object}} startCtx context | ||
| * @param {editor:{CodeMirror}, pos:{ch:{string}, line:{number}}, token:{object}} endCtx context | ||
| * @return {{start: {line: number, ch: number}, | ||
| * end: {line: number, ch: number}}} A range object. | ||
| */ | ||
| function _getRangeForPropValue(startCtx, endCtx) { | ||
| var range = { "start": {}, | ||
| "end": {} }; | ||
|
|
||
| // Skip the ":" and any leading whitespace | ||
| while (TokenUtils.moveNextToken(startCtx)) { | ||
| if (startCtx.token.string.trim().length > 0) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that this might be better another thing that i'm wondering if
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, linter will complain on the empty block. And you're mistaken with the direction. We're scanning forwards and not backwards here. |
||
| break; | ||
| } | ||
| } | ||
|
|
||
| // Skip the trailing whitespace and property separators. | ||
| while (endCtx.token.string === ";" || endCtx.token.string === "}" || | ||
| endCtx.token.string.trim().length === 0) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can just say |
||
| TokenUtils.movePrevToken(endCtx); | ||
| } | ||
|
|
||
| range.start = _.clone(startCtx.pos); | ||
| range.start.ch = startCtx.token.start; | ||
|
|
||
| range.end = _.clone(endCtx.pos); | ||
| range.end.ch = endCtx.token.end; | ||
|
|
||
| return range; | ||
| } | ||
|
|
||
| /** | ||
| * @private | ||
| * Returns a context info object for the current CSS style rule | ||
|
|
@@ -345,7 +390,9 @@ define(function (require, exports, module) { | |
| * name: string, | ||
| * index: number, | ||
| * values: Array.<string>, | ||
| * isNewItem: boolean}} A CSS context info object. | ||
| * isNewItem: boolean, | ||
| * range: {start: {line: number, ch: number}, | ||
| * end: {line: number, ch: number}}}} A CSS context info object. | ||
| */ | ||
| function _getRuleInfoStartingFromPropValue(ctx, editor) { | ||
| var propNamePos = $.extend({}, ctx.pos), | ||
|
|
@@ -361,7 +408,8 @@ define(function (require, exports, module) { | |
| canAddNewOne = false, | ||
| testPos = {ch: ctx.pos.ch + 1, line: ctx.pos.line}, | ||
| testToken = editor._codeMirror.getTokenAt(testPos, true), | ||
| propName; | ||
| propName, | ||
| range; | ||
|
|
||
| // Get property name first. If we don't have a valid property name, then | ||
| // return a default rule info. | ||
|
|
@@ -413,13 +461,15 @@ define(function (require, exports, module) { | |
| forwardCtx = TokenUtils.getInitialContext(editor._codeMirror, forwardPos); | ||
| propValues = propValues.concat(_getSucceedingPropValues(forwardCtx, lastValue)); | ||
|
|
||
| range = _getRangeForPropValue(backwardCtx, forwardCtx); | ||
|
|
||
| // If current index is more than the propValues size, then the cursor is | ||
| // at the end of the existing property values and is ready for adding another one. | ||
| if (index === propValues.length) { | ||
| canAddNewOne = true; | ||
| } | ||
|
|
||
| return createInfo(PROP_VALUE, offset, propName, index, propValues, canAddNewOne); | ||
| return createInfo(PROP_VALUE, offset, propName, index, propValues, canAddNewOne, range); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -432,7 +482,9 @@ define(function (require, exports, module) { | |
| * name: string, | ||
| * index: number, | ||
| * values: Array.<string>, | ||
| * isNewItem: boolean}} A CSS context info object. | ||
| * isNewItem: boolean, | ||
| * range: {start: {line: number, ch: number}, | ||
| * end: {line: number, ch: number}}}} A CSS context info object. | ||
| */ | ||
| function _getImportUrlInfo(ctx, editor) { | ||
| var propNamePos = $.extend({}, ctx.pos), | ||
|
|
@@ -501,7 +553,9 @@ define(function (require, exports, module) { | |
| * name: string, | ||
| * index: number, | ||
| * values: Array.<string>, | ||
| * isNewItem: boolean}} A CSS context info object. | ||
| * isNewItem: boolean, | ||
| * range: {start: {line: number, ch: number}, | ||
| * end: {line: number, ch: number}}}} A CSS context info object. | ||
| */ | ||
| function getInfoAtPos(editor, constPos) { | ||
| // We're going to be changing pos a lot, but we don't want to mess up | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* */ | ||
|
|
||
| .foo { | ||
| shape-inside: circle(0 | ||
| at | ||
| 0 0 | ||
| ); | ||
|
|
||
| shape-inside: circle (0px | ||
| at | ||
| 0px | ||
|
|
||
| 0px | ||
| ); | ||
|
|
||
| shape-inside: polygon(0 0, | ||
| 100px | ||
|
|
||
| 0, | ||
| 100px 100px | ||
|
|
||
|
|
||
| ); | ||
|
|
||
|
|
||
| shape-inside: | ||
| polygon( | ||
|
|
||
|
|
||
| nonzero, | ||
| 0 0, | ||
| 100px | ||
| 0, | ||
| 100px | ||
|
|
||
| 100px | ||
| ); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| @keyframes colorize { | ||
| 0% { | ||
| -webkit-filter: grayscale(100%); | ||
| } | ||
| 100% { | ||
| -webkit-filter: | ||
|
|
||
|
|
||
| grayscale | ||
| ( 0 % ) | ||
|
|
||
| ; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the conditional? can't
isNewItemjust just be assigned to the argument coming in? you could doisNewItem: (isNewItem === true)which is more performant since it's just a simple math eval