提交 b7a8a4aa 编写于 作者: B Benjamin Pasero

untitled - do not pick first line name if it does not include words

上级 6d5a1e82
...@@ -18,6 +18,7 @@ import { IResolvedTextEditorModel } from 'vs/editor/common/services/resolverServ ...@@ -18,6 +18,7 @@ import { IResolvedTextEditorModel } from 'vs/editor/common/services/resolverServ
import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService'; import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService'; import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { ensureValidWordDefinition } from 'vs/editor/common/model/wordHelper';
/** /**
* An editor input to be used for untitled text buffers. * An editor input to be used for untitled text buffers.
...@@ -34,7 +35,7 @@ export class UntitledTextEditorInput extends TextEditorInput implements IEncodin ...@@ -34,7 +35,7 @@ export class UntitledTextEditorInput extends TextEditorInput implements IEncodin
readonly onDidModelChangeEncoding = this._onDidModelChangeEncoding.event; readonly onDidModelChangeEncoding = this._onDidModelChangeEncoding.event;
private cachedModel: UntitledTextEditorModel | null = null; private cachedModel: UntitledTextEditorModel | null = null;
private cachedModelFirstLine: string | undefined = undefined; private cachedModelFirstWords: string | undefined = undefined;
private modelResolve: Promise<UntitledTextEditorModel & IResolvedTextEditorModel> | null = null; private modelResolve: Promise<UntitledTextEditorModel & IResolvedTextEditorModel> | null = null;
...@@ -76,11 +77,11 @@ export class UntitledTextEditorInput extends TextEditorInput implements IEncodin ...@@ -76,11 +77,11 @@ export class UntitledTextEditorInput extends TextEditorInput implements IEncodin
getName(): string { getName(): string {
// Take name from first line if present and only if // Take name from first words if present and only if
// we have no associated file path. In that case we // we have no associated file path. In that case we
// prefer the file name as title. // prefer the file name as title.
if (!this._hasAssociatedFilePath && this.cachedModelFirstLine) { if (!this._hasAssociatedFilePath && this.cachedModelFirstWords) {
return this.cachedModelFirstLine; return this.cachedModelFirstWords;
} }
// Otherwise fallback to resource // Otherwise fallback to resource
...@@ -302,9 +303,21 @@ export class UntitledTextEditorInput extends TextEditorInput implements IEncodin ...@@ -302,9 +303,21 @@ export class UntitledTextEditorInput extends TextEditorInput implements IEncodin
} }
private onDidChangeFirstLine(model: UntitledTextEditorModel): void { private onDidChangeFirstLine(model: UntitledTextEditorModel): void {
// Determine the first words of the model following these rules:
// - cannot be only whitespace (so we trim())
// - cannot be only non-alphanumeric characters (so we run word definition regex over it)
// - cannot be longer than FIRST_LINE_MAX_TITLE_LENGTH
let modelFirstWordsCandidate: string | undefined = undefined;
const firstLineText = model.textEditorModel?.getValueInRange({ startLineNumber: 1, endLineNumber: 1, startColumn: 1, endColumn: UntitledTextEditorInput.FIRST_LINE_MAX_TITLE_LENGTH }).trim(); const firstLineText = model.textEditorModel?.getValueInRange({ startLineNumber: 1, endLineNumber: 1, startColumn: 1, endColumn: UntitledTextEditorInput.FIRST_LINE_MAX_TITLE_LENGTH }).trim();
if (firstLineText !== this.cachedModelFirstLine) { if (firstLineText && ensureValidWordDefinition().exec(firstLineText)) {
this.cachedModelFirstLine = firstLineText; modelFirstWordsCandidate = firstLineText;
}
if (modelFirstWordsCandidate !== this.cachedModelFirstWords) {
this.cachedModelFirstWords = modelFirstWordsCandidate;
this._onDidChangeLabel.fire(); this._onDidChangeLabel.fire();
} }
} }
......
...@@ -387,10 +387,19 @@ suite('Workbench untitled text editors', () => { ...@@ -387,10 +387,19 @@ suite('Workbench untitled text editors', () => {
model.textEditorModel.setValue(''); model.textEditorModel.setValue('');
assert.equal(input.getName(), 'Untitled-1'); assert.equal(input.getName(), 'Untitled-1');
assert.equal(counter, 3); model.textEditorModel.setValue(' ');
assert.equal(input.getName(), 'Untitled-1');
model.textEditorModel.setValue('([]}'); // require actual words
assert.equal(input.getName(), 'Untitled-1');
model.textEditorModel.setValue('([]}hello '); // require actual words
assert.equal(input.getName(), '([]}hello');
assert.equal(counter, 6);
model.textEditorModel.setValue('Hello\nWorld'); model.textEditorModel.setValue('Hello\nWorld');
assert.equal(counter, 4); assert.equal(counter, 7);
function createSingleEditOp(text: string, positionLineNumber: number, positionColumn: number, selectionLineNumber: number = positionLineNumber, selectionColumn: number = positionColumn): IIdentifiedSingleEditOperation { function createSingleEditOp(text: string, positionLineNumber: number, positionColumn: number, selectionLineNumber: number = positionLineNumber, selectionColumn: number = positionColumn): IIdentifiedSingleEditOperation {
let range = new Range( let range = new Range(
...@@ -409,7 +418,7 @@ suite('Workbench untitled text editors', () => { ...@@ -409,7 +418,7 @@ suite('Workbench untitled text editors', () => {
} }
model.textEditorModel.applyEdits([createSingleEditOp('hello', 2, 2)]); model.textEditorModel.applyEdits([createSingleEditOp('hello', 2, 2)]);
assert.equal(counter, 4); // change was not on first line assert.equal(counter, 7); // change was not on first line
input.dispose(); input.dispose();
model.dispose(); model.dispose();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册