Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #7498 from adobe/randy/issue-6244
Browse files Browse the repository at this point in the history
Smarter updating of URL Hints - don't overwrite filename when inserting a directory before it
  • Loading branch information
peterflynn committed May 7, 2014
2 parents f7d8e6c + 4dc4f23 commit 6014714
Show file tree
Hide file tree
Showing 3 changed files with 412 additions and 38 deletions.
59 changes: 41 additions & 18 deletions src/extensions/default/UrlCodeHints/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,17 +358,20 @@ define(function (require, exports, module) {
query = "";
}

var hintsAndSortFunc = this._getUrlHints({queryStr: query});
var hints = hintsAndSortFunc.hints;
var hintsAndSortFunc = this._getUrlHints({queryStr: query}),
hints = hintsAndSortFunc.hints;

if (hints instanceof Array) {
// If we got synchronous hints, check if we have something we'll actually use
var i, foundPrefix = false;
query = query.toLowerCase();
for (i = 0; i < hints.length; i++) {
if (hints[i].indexOf(query) === 0) {
if (hints[i].toLowerCase().indexOf(query) === 0) {
foundPrefix = true;
break;
}
}

if (!foundPrefix) {
query = null;
}
Expand Down Expand Up @@ -479,9 +482,10 @@ define(function (require, exports, module) {

if (hints instanceof Array && hints.length) {
// Array was returned
var lowerCaseFilter = filter.toLowerCase();
console.assert(!result.length);
result = $.map(hints, function (item) {
if (item.indexOf(filter) === 0) {
if (item.toLowerCase().indexOf(lowerCaseFilter) === 0) {
return item;
}
}).sort(sortFunc);
Expand All @@ -497,8 +501,9 @@ define(function (require, exports, module) {
// Deferred hints were returned
var deferred = $.Deferred();
hints.done(function (asyncHints) {
var lowerCaseFilter = filter.toLowerCase();
result = $.map(asyncHints, function (item) {
if (item.indexOf(filter) === 0) {
if (item.toLowerCase().indexOf(lowerCaseFilter) === 0) {
return item;
}
}).sort(sortFunc);
Expand Down Expand Up @@ -656,9 +661,19 @@ define(function (require, exports, module) {
hasClosingParen = (closingPos.index !== -1);
}

// Adjust insert char positions to replace existing value, if there is a closing paren
if (closingPos.index !== -1) {
end.ch += this.getCharOffset(this.info.values, this.info, closingPos);
// Insert folder names, but replace file names, so if a file is selected
// (i.e. closeOnSelect === true), then adjust insert char positions to
// replace existing value, if there is a closing paren
if (this.closeOnSelect) {
if (closingPos.index !== -1) {
end.ch += this.getCharOffset(this.info.values, this.info, closingPos);
}
} else {
// If next char is "/", then overwrite it since we're inserting a "/"
var nextSlash = this.findNextPosInArray(this.info.values, "/", this.info);
if (nextSlash.index === this.info.index && nextSlash.offset === this.info.offset) {
end.ch += 1;
}
}
if (this.info.filter.length > 0) {
start.ch -= this.info.filter.length;
Expand Down Expand Up @@ -721,15 +736,16 @@ define(function (require, exports, module) {
charCount = 0,
replaceExistingOne = tagInfo.attr.valueAssigned,
endQuote = "",
shouldReplace = true;
shouldReplace = false;

if (tokenType === HTMLUtils.ATTR_VALUE) {
charCount = tagInfo.attr.value.length;

// Special handling for URL hinting -- if the completion is a file name
// and not a folder, then close the code hint list.
if (!this.closeOnSelect && completion.match(/\/$/) === null) {
this.closeOnSelect = true;

// Insert folder names, but replace file names
shouldReplace = true;
}

if (!tagInfo.attr.hasEndQuote) {
Expand All @@ -742,19 +758,26 @@ define(function (require, exports, module) {
} else if (completion === tagInfo.attr.value) {
shouldReplace = false;
}

if (shouldReplace) {
// Replace entire value
charCount = tagInfo.attr.value.length;
} else {
// Replace filter (to insert new selection)
charCount = this.info.filter.length;

// If next char is "/", then overwrite it since we're inserting a "/"
if (this.info.attr.value.length > charCount && this.info.attr.value[charCount] === "/") {
charCount += 1;
}
}
}

end.line = start.line = cursor.line;
start.ch = cursor.ch - tagInfo.position.offset;
end.ch = start.ch + charCount;

if (shouldReplace) {
if (start.ch !== end.ch) {
this.editor.document.replaceRange(completion, start, end);
} else {
this.editor.document.replaceRange(completion, start);
}
}
this.editor.document.replaceRange(completion, start, end);

if (!this.closeOnSelect) {
// If we append the missing quote, then we need to adjust the cursor postion
Expand Down
3 changes: 3 additions & 0 deletions src/extensions/default/UrlCodeHints/testfiles/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
background-image: url();
border-image: url('');
list-style-image: url("");
filter: url("/");
background-image: url(dummy.jpg);
background-image:
}
</style>
Expand All @@ -19,5 +21,6 @@
<a href='results.html?id=1003'>Results</a>
<a href='../'>Page relative: up 1 folder</a>
<a href='/'>Site-root relative: root folder</a>
<a href='test2.html'>Results</a>
</body>
</html>
Loading

0 comments on commit 6014714

Please sign in to comment.