未验证 提交 c850f071 编写于 作者: J Joao Moreno

fixes #81798

上级 a8fc2bd9
......@@ -117,6 +117,8 @@ export class PathIterator implements IKeyIterator {
private _from!: number;
private _to!: number;
constructor(private _splitOnBackslash: boolean = true) { }
reset(key: string): this {
this._value = key.replace(/\\$|\/$/, '');
this._from = 0;
......@@ -134,7 +136,7 @@ export class PathIterator implements IKeyIterator {
let justSeps = true;
for (; this._to < this._value.length; this._to++) {
const ch = this._value.charCodeAt(this._to);
if (ch === CharCode.Slash || ch === CharCode.Backslash) {
if (ch === CharCode.Slash || this._splitOnBackslash && ch === CharCode.Backslash) {
if (justSeps) {
this._from++;
} else {
......
......@@ -9,6 +9,7 @@ import { Iterator } from 'vs/base/common/iterator';
import { relativePath, joinPath } from 'vs/base/common/resources';
import { URI } from 'vs/base/common/uri';
import { mapValues } from 'vs/base/common/collections';
import { PathIterator } from 'vs/base/common/map';
export interface ILeafNode<T, C = void> {
readonly uri: URI;
......@@ -113,18 +114,18 @@ export class ResourceTree<T extends NonNullable<any>, C> {
add(uri: URI, element: T): void {
const key = relativePath(this.root.uri, uri) || uri.fsPath;
const parts = key.split(/[\\\/]/).filter(p => !!p);
const iterator = new PathIterator(false).reset(key);
let node = this.root;
let path = '';
for (let i = 0; i < parts.length; i++) {
const name = parts[i];
while (true) {
const name = iterator.value();
path = path + '/' + name;
let child = node.get(name);
if (!child) {
if (i < parts.length - 1) {
if (iterator.hasNext()) {
child = new BranchNode(joinPath(this.root.uri, path), path, this.root.context, node);
node.set(name, child);
} else {
......@@ -135,18 +136,24 @@ export class ResourceTree<T extends NonNullable<any>, C> {
}
if (!(child instanceof BranchNode)) {
if (i < parts.length - 1) {
if (iterator.hasNext()) {
throw new Error('Inconsistent tree: can\'t override leaf with branch.');
}
// replace
node.set(name, new LeafNode(uri, path, this.root.context, element));
return;
} else if (i === parts.length - 1) {
} else if (!iterator.hasNext()) {
throw new Error('Inconsistent tree: can\'t override branch with leaf.');
}
node = child;
if (!iterator.hasNext()) {
return;
}
iterator.next();
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册