This commit is contained in:
Johannes Rieken 2018-06-06 16:51:14 +02:00
parent b4cd62c803
commit fe9ae5f426
3 changed files with 47 additions and 3 deletions

View file

@ -33,10 +33,14 @@ export class HighlightedLabel implements IDisposable {
return this.domNode;
}
set(text: string, highlights: IHighlight[] = [], title: string = '') {
set(text: string, highlights: IHighlight[] = [], title: string = '', escapeNewLines?: boolean) {
if (!text) {
text = '';
}
if (escapeNewLines) {
// adjusts highlights inplace
text = HighlightedLabel.escapeNewLines(text, highlights);
}
if (this.didEverRender && this.text === text && this.title === title && objects.equals(this.highlights, highlights)) {
return;
}
@ -90,4 +94,30 @@ export class HighlightedLabel implements IDisposable {
this.text = null;
this.highlights = null;
}
static escapeNewLines(text: string, highlights: IHighlight[]): string {
let total = 0;
let extra = 0;
return text.replace(/\r\n|\r|\n/, (match, offset) => {
extra = match === '\r\n' ? -1 : 0;
offset += total;
for (const highlight of highlights) {
if (highlight.end <= offset) {
continue;
}
if (highlight.start >= offset) {
highlight.start += extra;
}
if (highlight.end >= offset) {
highlight.end += extra;
}
}
total += extra;
return '\u23CE';
});
}
}

View file

@ -52,4 +52,18 @@ suite('HighlightedLabel', () => {
label.set('foobarfoo', [{ start: 3, end: 6 }]);
assert.equal(label.element.innerHTML, '<span>foo</span><span class="highlight">bar</span><span>foo</span>');
});
});
test('escapeNewLines', function () {
let highlights = [{ start: 0, end: 5 }, { start: 7, end: 9 }, { start: 11, end: 12 }];// before,after,after
let escaped = HighlightedLabel.escapeNewLines('ACTION\r\n_TYPE2', highlights);
assert.equal(escaped, 'ACTION\u23CE_TYPE2');
assert.deepEqual(highlights, [{ start: 0, end: 5 }, { start: 6, end: 8 }, { start: 10, end: 11 }]);
highlights = [{ start: 5, end: 9 }, { start: 11, end: 12 }];//overlap,after
escaped = HighlightedLabel.escapeNewLines('ACTION\r\n_TYPE2', highlights);
assert.equal(escaped, 'ACTION\u23CE_TYPE2');
assert.deepEqual(highlights, [{ start: 5, end: 8 }, { start: 10, end: 11 }]);
});
});

View file

@ -152,7 +152,7 @@ class Renderer implements IRenderer<ICompletionItem, ISuggestionTemplateData> {
}
}
data.highlightedLabel.set(suggestion.label, createMatches(element.matches));
data.highlightedLabel.set(suggestion.label, createMatches(element.matches), '', true);
// data.highlightedLabel.set(`${suggestion.label} <${element.score}=score(${element.word}, ${suggestion.filterText || suggestion.label})>`, createMatches(element.matches));
data.typeLabel.textContent = (suggestion.detail || '').replace(/\n.*$/m, '');