vscode#DocumentSymbol TypeScript Examples

The following examples show how to use vscode#DocumentSymbol. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ObjIdConfigLinter.ts    From al-objid with MIT License 6 votes vote down vote up
private getPropertyPromise(name: string) {
        return new Promise<DocumentSymbol | undefined>(resolve => {
            this._symbolsPromise.then(symbols => {
                if (!symbols) {
                    resolve(undefined);
                    return;
                }

                const property = symbols.find(symbol => symbol.name === name)!;
                resolve(property);
            });
        });
    }
Example #2
Source File: getSymbolAtPosition.ts    From al-objid with MIT License 6 votes vote down vote up
export async function getSymbolAtPosition(
    uri: Uri,
    position: Position,
    matches?: DocumentSymbol[]
): Promise<DocumentSymbol | null> {
    if (!matches) {
        matches = [];
    }
    try {
        const symbols: DocumentSymbol[] = (await commands.executeCommand(
            CodeCommand.ExecuteDocumentSymbolProvider,
            uri
        )) as DocumentSymbol[];
        return getSymbolInChildren(position, symbols, null, matches);
    } catch {}
    return null;
}
Example #3
Source File: getSymbolAtPosition.ts    From al-objid with MIT License 6 votes vote down vote up
function getSymbolInChildren(
    position: Position,
    symbols: DocumentSymbol[],
    bestMatch: DocumentSymbol | null,
    matches: DocumentSymbol[]
): DocumentSymbol | null {
    for (let symbol of symbols) {
        if (symbol.range.start.isBeforeOrEqual(position) && symbol.range.end.isAfterOrEqual(position)) {
            matches.push(symbol);
            bestMatch = getBestMatch(symbol, bestMatch);
        }
    }
    for (let symbol of symbols) {
        if (!symbol.children || !symbol.children.length) continue;
        let result = getSymbolInChildren(position, symbol.children, bestMatch, matches);
        if (result) {
            bestMatch = getBestMatch(result, bestMatch);
        }
    }
    return bestMatch;
}
Example #4
Source File: getSymbolAtPosition.ts    From al-objid with MIT License 6 votes vote down vote up
function getBestMatch(checkSymbol: DocumentSymbol, bestMatch: DocumentSymbol | null): DocumentSymbol {
    if (!bestMatch) {
        return checkSymbol;
    }
    return checkSymbol.range.start.isAfterOrEqual(bestMatch.range.start) &&
        checkSymbol.range.end.isBeforeOrEqual(bestMatch.range.end)
        ? checkSymbol
        : bestMatch;
}
Example #5
Source File: ObjIdConfigSymbols.ts    From al-objid with MIT License 6 votes vote down vote up
constructor(uri: Uri) {
        if (!fs.existsSync(uri.fsPath)) {
            this._symbolsPromise = Promise.resolve(undefined);
            this._idRangesPromise = Promise.resolve(undefined);
            this._objectRangesPromise = Promise.resolve(undefined);
            this._bcLicensePromise = Promise.resolve(undefined);
            return;
        }

        this._symbolsPromise = new Promise(async resolve => {
            if (!(await jsonAvailable)) {
                resolve(undefined);
                return;
            }

            const symbols = await (commands.executeCommand(CodeCommand.ExecuteDocumentSymbolProvider, uri) as Promise<
                DocumentSymbol[] | undefined
            >);
            resolve(symbols);
        });

        this._idRangesPromise = this.getPropertyPromise(ConfigurationProperty.Ranges);
        this._objectRangesPromise = this.getPropertyPromise(ConfigurationProperty.ObjectRanges);
        this._bcLicensePromise = this.getPropertyPromise(ConfigurationProperty.BcLicense);
    }
Example #6
Source File: ObjIdConfigSymbols.ts    From al-objid with MIT License 6 votes vote down vote up
private getPropertyPromise(name: string) {
        return new Promise<DocumentSymbol | undefined>(resolve => {
            this._symbolsPromise.then(symbols => {
                if (!symbols) {
                    resolve(undefined);
                    return;
                }

                const property = symbols.find(symbol => symbol.name === name)!;
                resolve(property);
            });
        });
    }
Example #7
Source File: ObjIdConfigLinter.ts    From al-objid with MIT License 6 votes vote down vote up
constructor(uri: Uri) {
        this._uri = uri;

        if (!fs.existsSync(uri.fsPath)) {
            this._symbolsPromise = Promise.resolve(undefined);
            this._idRangesPromise = Promise.resolve(undefined);
            this._objectRangesPromise = Promise.resolve(undefined);
            this._bcLicensePromise = Promise.resolve(undefined);
            this._authKeyPromise = Promise.resolve(undefined);
            this._appPoolIdPromise = Promise.resolve(undefined);
            return;
        }

        this._symbolsPromise = new Promise(async resolve => {
            if (!(await jsonAvailable)) {
                resolve(undefined);
                return;
            }

            const symbols = await (commands.executeCommand(CodeCommand.ExecuteDocumentSymbolProvider, uri) as Promise<
                DocumentSymbol[] | undefined
            >);
            resolve(symbols);
        });

        this._idRangesPromise = this.getPropertyPromise(ConfigurationProperty.Ranges);
        this._objectRangesPromise = this.getPropertyPromise(ConfigurationProperty.ObjectRanges);
        this._bcLicensePromise = this.getPropertyPromise(ConfigurationProperty.BcLicense);
        this._authKeyPromise = this.getPropertyPromise(ConfigurationProperty.AuthKey);
        this._appPoolIdPromise = this.getPropertyPromise(ConfigurationProperty.AppPoolId);
    }
Example #8
Source File: ObjIdConfigLinter.ts    From al-objid with MIT License 6 votes vote down vote up
private makeSurePropertyIsValidPositiveInteger(
        value: number,
        symbol: DocumentSymbol,
        diagnose: CreateDiagnostic
    ): boolean {
        if (!value || value !== parseFloat(symbol.detail) || value < 0) {
            diagnose(
                this.getDetailRange(symbol),
                `A valid positive integer expected`,
                DiagnosticSeverity.Error,
                DIAGNOSTIC_CODE.OBJIDCONFIG.IDRANGES_INVALID_NUMBER
            );
            return false;
        }

        return true;
    }
Example #9
Source File: ObjIdConfigLinter.ts    From al-objid with MIT License 6 votes vote down vote up
private makeSurePropertyIsDefined(
        property: DocumentSymbol | undefined,
        symbol: DocumentSymbol,
        name: string,
        diagnose: CreateDiagnostic
    ): boolean {
        if (!property) {
            diagnose(
                symbol.selectionRange,
                `Missing property: ${name}`,
                DiagnosticSeverity.Error,
                DIAGNOSTIC_CODE.OBJIDCONFIG.MISSING_PROPERTY
            );
            return false;
        }

        return true;
    }
Example #10
Source File: ObjIdConfigLinter.ts    From al-objid with MIT License 6 votes vote down vote up
private expectType(
        symbol: DocumentSymbol,
        expectedKind: SymbolKind,
        type: string,
        diagnose: CreateDiagnostic
    ): boolean {
        if (symbol.kind !== expectedKind) {
            diagnose(
                this.getDetailRange(symbol),
                `${type} expected`,
                DiagnosticSeverity.Error,
                DIAGNOSTIC_CODE.OBJIDCONFIG.IDRANGES_INVALID_TYPE
            );
            return false;
        }

        return true;
    }
Example #11
Source File: spDefineItem.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
toDocumentSymbol(): DocumentSymbol | undefined {
    if (this.fullRange === undefined) {
      return undefined;
    }
    return new DocumentSymbol(
      this.name,
      this.description ? this.description.replace(/^\*\</, "") : "",
      SymbolKind.Constant,
      this.fullRange,
      this.range
    );
  }
Example #12
Source File: goto-definition.ts    From al-objid with MIT License 6 votes vote down vote up
async function getNamedLogicalRanges(uri: Uri, name: string): Promise<DocumentSymbol[] | undefined> {
    const idRanges = await getIdRanges(uri);
    if (!idRanges) {
        return;
    }
    const result: DocumentSymbol[] = [];
    for (let range of idRanges!.children) {
        if (
            range.children.find(c => c.name === "description" && c.detail === name) ||
            (!name && !range.children.find(c => c.name === "description"))
        ) {
            result.push(range);
        }
    }
    return result;
}
Example #13
Source File: goto-definition.ts    From al-objid with MIT License 6 votes vote down vote up
async function getObjectRanges(uri: Uri): Promise<DocumentSymbol | undefined> {
    await jsonAvailable;

    const symbols = await (commands.executeCommand(CodeCommand.ExecuteDocumentSymbolProvider, uri) as Promise<
        DocumentSymbol[] | undefined
    >);
    if (!symbols) {
        return;
    }
    return symbols.find(symbol => symbol.name === "objectRanges");
}
Example #14
Source File: goto-definition.ts    From al-objid with MIT License 6 votes vote down vote up
async function getIdRanges(uri: Uri): Promise<DocumentSymbol | undefined> {
    await jsonAvailable;

    const symbols = await (commands.executeCommand(CodeCommand.ExecuteDocumentSymbolProvider, uri) as Promise<
        DocumentSymbol[] | undefined
    >);
    if (!symbols) {
        return;
    }
    return symbols.find(symbol => symbol.name === "idRanges");
}
Example #15
Source File: CodeLinkFeature.ts    From vscode-drawio with GNU General Public License v3.0 6 votes vote down vote up
async function resolveSymbol(
	uri: Uri,
	path: string
): Promise<Range | undefined> {
	const result = (await commands.executeCommand(
		"vscode.executeDocumentSymbolProvider",
		uri
	)) as DocumentSymbol[];
	let treePath = path.split(".");
	let cur: DocumentSymbol[] | undefined = result;
	for (let i = 0; i < treePath.length; i++) {
		if (cur == undefined) break;
		cur = cur.filter((x) => x.name == treePath[i]);
		if (i < treePath.length - 1) cur = cur[0]?.children;
	}
	if (cur == undefined || cur.length == 0) return undefined;
	return cur[0].selectionRange;
}
Example #16
Source File: spEnumItem.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
toDocumentSymbol(): DocumentSymbol | undefined {
    if (this.fullRange === undefined) {
      return undefined;
    }
    return new DocumentSymbol(
      this.name.replace(/Enum#(\d+)/, "Anonymous$1"),
      this.description,
      SymbolKind.Enum,
      this.fullRange,
      this.range
    );
  }
Example #17
Source File: spFunctionItem.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
toDocumentSymbol(): DocumentSymbol | undefined {
    if (this.fullRange === undefined) {
      return undefined;
    }
    return new DocumentSymbol(
      this.name,
      this.description,
      SymbolKind.Function,
      this.fullRange,
      this.range
    );
  }
Example #18
Source File: spMethodItem.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
toDocumentSymbol(): DocumentSymbol | undefined {
    if (this.fullRange === undefined) {
      return undefined;
    }
    return new DocumentSymbol(
      this.name,
      this.description,
      SymbolKind.Method,
      this.fullRange,
      this.range
    );
  }
Example #19
Source File: spMethodmapItem.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
toDocumentSymbol(): DocumentSymbol | undefined {
    if (this.fullRange === undefined) {
      return undefined;
    }
    return new DocumentSymbol(
      this.name,
      this.description,
      SymbolKind.Class,
      this.fullRange,
      this.range
    );
  }
Example #20
Source File: spPropertyItem.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
toDocumentSymbol(): DocumentSymbol | undefined {
    if (this.fullRange === undefined) {
      return undefined;
    }
    return new DocumentSymbol(
      this.name,
      this.description,
      SymbolKind.Property,
      this.fullRange,
      this.range
    );
  }
Example #21
Source File: spTypedefItem.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
toDocumentSymbol(): DocumentSymbol | undefined {
    if (this.fullRange === undefined) {
      return undefined;
    }
    return new DocumentSymbol(
      this.name,
      this.description,
      SymbolKind.TypeParameter,
      this.fullRange,
      this.range
    );
  }
Example #22
Source File: spTypesetItem.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
toDocumentSymbol(): DocumentSymbol | undefined {
    if (this.fullRange === undefined) {
      return undefined;
    }
    return new DocumentSymbol(
      this.name,
      this.description,
      SymbolKind.TypeParameter,
      this.fullRange,
      this.range
    );
  }
Example #23
Source File: spEnumStructItem.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
toDocumentSymbol(): DocumentSymbol | undefined {
    if (this.fullRange === undefined) {
      return undefined;
    }
    return new DocumentSymbol(
      this.name,
      this.description,
      SymbolKind.Struct,
      this.fullRange,
      this.range
    );
  }
Example #24
Source File: spVariableItem.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
toDocumentSymbol(): DocumentSymbol {
    return new DocumentSymbol(
      this.name,
      this.type,
      SymbolKind.Variable,
      this.range,
      this.range
    );
  }
Example #25
Source File: spEnumMemberItem.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
toDocumentSymbol(): DocumentSymbol | undefined {
    if (this.name === "") {
      return undefined;
    }
    return new DocumentSymbol(
      this.name,
      this.description.replace(/^\*\</, ""),
      SymbolKind.Enum,
      this.range,
      this.range
    );
  }
Example #26
Source File: spSymbolProvider.ts    From sourcepawn-vscode with MIT License 6 votes vote down vote up
export function symbolProvider(
  itemsRepo: ItemsRepository,
  document: TextDocument,
  token: CancellationToken
): DocumentSymbol[] {
  let symbols: DocumentSymbol[] = [];
  let items = itemsRepo.getAllItems(document.uri);
  let file = document.uri.fsPath;
  for (let item of items) {
    if (allowedKinds.includes(item.kind) && item.filePath === file) {
      // Don't add non global variables here
      if (
        item.kind === CompletionItemKind.Variable &&
        item.parent !== globalItem
      ) {
        continue;
      }
      let symbol = item.toDocumentSymbol();

      // Check if the item can have childrens
      if (allowedParentsKinds.includes(item.kind) && symbol !== undefined) {
        symbol.children = items
          .filter(
            (e) =>
              allowedChildrendKinds.includes(e.kind) &&
              e.filePath === file &&
              e.parent === item
          )
          .map((e) => e.toDocumentSymbol())
          .filter((e) => e !== undefined);
      }
      if (symbol !== undefined) {
        symbols.push(symbol);
      }
    }
  }
  return symbols;
}
Example #27
Source File: ObjIdConfigSymbols.ts    From al-objid with MIT License 5 votes vote down vote up
private readonly _bcLicensePromise: Promise<DocumentSymbol | undefined>;
Example #28
Source File: ObjIdConfigSymbols.ts    From al-objid with MIT License 5 votes vote down vote up
public properties = {
        idRanges: (range: DocumentSymbol) => ({
            ...range,
            from: () => range.children.find(child => child.name === "from"),
            to: () => range.children.find(child => child.name === "to"),
        }),
    };
Example #29
Source File: ObjIdConfigSymbols.ts    From al-objid with MIT License 5 votes vote down vote up
private readonly _objectRangesPromise: Promise<DocumentSymbol | undefined>;