Skip to content

Commit

Permalink
Improve list scope goto #10
Browse files Browse the repository at this point in the history
  • Loading branch information
RedCMD committed Jun 8, 2024
1 parent 1c34462 commit baf548c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 72 deletions.
8 changes: 6 additions & 2 deletions src/textmate/grammar/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,12 +980,14 @@ export class LineTokens {
}

public produce(stack: StateStackImpl, endIndex: number): void {
this.produceFromScopes(stack.contentNameScopesList, endIndex);
// @ts-ignore
this.produceFromScopes(stack.contentNameScopesList, endIndex, stack.ruleId);
}

public produceFromScopes(
scopesList: AttributedScopeStack | null,
endIndex: number
endIndex: number,
ruleId?: RuleId,
): void {
if (this._lastTokenEndIndex >= endIndex) {
return;
Expand Down Expand Up @@ -1065,6 +1067,8 @@ export class LineTokens {
startIndex: this._lastTokenEndIndex,
endIndex: endIndex,
// value: lineText.substring(lastTokenEndIndex, endIndex),
// @ts-ignore
ruleId: ruleId,
scopes: scopes
});

Expand Down
57 changes: 30 additions & 27 deletions src/textmate/grammar/tokenizeString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,31 +326,22 @@ export function _tokenizeString(
isFirstLine = false;
}


// // @ts-ignore
// console.log(grammar._lastRuleId);
// @ts-ignore
if (r) {
// @ts-ignore
grammar.rules.push(
{
captureIndices: captureIndices,
matchedRuleId: matchedRuleId == -1 ? -poppedRule.id : matchedRuleId, // While was `-2`!!
// ...r,
// ...{
// lineText: lineText,
// isFirstLine: isFirstLine,
// linePos: linePos,
// stack: stack,
anchorPosition: anchorPosition,
time: performance.now(),
// @ts-ignore
// length: grammar._ruleId2desc.length - 1,
// lastRuleId: grammar._lastRuleId,
// }
}
);
}
grammar.rules.push(
{
captureIndices: captureIndices,
matchedRuleId: matchedRuleId == endRuleId ? -poppedRule.id : matchedRuleId,
// lineText: lineText,
// isFirstLine: isFirstLine,
// linePos: linePos,
// stack: stack,
anchorPosition: anchorPosition,
time: performance.now(),
// @ts-ignore
// length: grammar._ruleId2desc.length - 1,
// lastRuleId: grammar._lastRuleId,
}
);
}
}

Expand Down Expand Up @@ -403,6 +394,15 @@ function _checkWhileConditions(grammar: Grammar, lineText: OnigString, isFirstLi
isFirstLine = false;
}
}
// @ts-ignore
grammar.rules.push(
{
captureIndices: r.captureIndices,
matchedRuleId: -whileRule.rule.id,
anchorPosition: anchorPosition,
time: performance.now(),
}
);
} else {
if (DebugFlags.InDebugMode) {
console.log(' popping ' + whileRule.rule.debugName + ' - ' + whileRule.rule.debugWhileRegExp);
Expand Down Expand Up @@ -618,12 +618,14 @@ function handleCaptures(grammar: Grammar, lineText: OnigString, isFirstLine: boo
// pop captures while needed
while (localStack.length > 0 && localStack[localStack.length - 1].endPos <= captureIndex.start) {
// pop!
lineTokens.produceFromScopes(localStack[localStack.length - 1].scopes, localStack[localStack.length - 1].endPos);
// @ts-ignore
lineTokens.produceFromScopes(localStack[localStack.length - 1].scopes, localStack[localStack.length - 1].endPos, stack.ruleId);
localStack.pop();
}

if (localStack.length > 0) {
lineTokens.produceFromScopes(localStack[localStack.length - 1].scopes, captureIndex.start);
// @ts-ignore
lineTokens.produceFromScopes(localStack[localStack.length - 1].scopes, captureIndex.start, stack.ruleId);
} else {
lineTokens.produce(stack, captureIndex.start);
}
Expand Down Expand Up @@ -653,7 +655,8 @@ function handleCaptures(grammar: Grammar, lineText: OnigString, isFirstLine: boo

while (localStack.length > 0) {
// pop!
lineTokens.produceFromScopes(localStack[localStack.length - 1].scopes, localStack[localStack.length - 1].endPos);
// @ts-ignore
lineTokens.produceFromScopes(localStack[localStack.length - 1].scopes, localStack[localStack.length - 1].endPos, stack.ruleId);
localStack.pop();
}
}
Expand Down
55 changes: 12 additions & 43 deletions src/treeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,26 +321,6 @@ export const TreeDataProvider: vscode.TreeDataProvider<element> = {
item.iconPath = new vscode.ThemeIcon('symbol-key', new vscode.ThemeColor('symbolIcon.stringForeground'));
item.tooltip = `Line: ${line + 1}`;

// const locations: vscode.Location[] = [];
// const range = new vscode.Range(line, 0, line + 1, 0);
// const location = new vscode.Location(
// document.uri,
// range,
// );
// locations.push(location);
// const position = new vscode.Position(line, 0);
// const command: vscode.Command = {
// title: `title`,
// tooltip: `tooltip`,
// command: 'editor.action.goToLocations',
// arguments: [
// document.uri,
// position,
// locations,
// ]
// };
// item.command = command;

return item;
}
if (id == 1) {
Expand Down Expand Up @@ -372,22 +352,6 @@ export const TreeDataProvider: vscode.TreeDataProvider<element> = {

item.tooltip = document.lineAt(line).text.substring(token.startIndex, token.endIndex - newLineAdjust);

// const location = new vscode.Location(
// document.uri,
// new vscode.Range(line, token.startIndex, line, token.endIndex),
// );
// const command: vscode.Command = {
// title: `title`,
// tooltip: `tooltip`,
// command: 'editor.action.goToLocations',
// arguments: [
// document.uri,
// new vscode.Position(line, token.startIndex),
// [location],
// ]
// };
// item.command = command;

return item;
}
if (id == 2) {
Expand Down Expand Up @@ -762,7 +726,7 @@ async function gotoGrammar(element: element) {
// annoyingly the ids are assigned on a first-served basis
// including across included/embedded grammars

let ruleId = 1;
let ruleId: RuleId;
let capturesIndex = -1;

if (element.type == 'tree') {
Expand All @@ -789,12 +753,14 @@ async function gotoGrammar(element: element) {
break;
}
let foundRule = false;
let captureIndex = 0;
let captureIndex = -1;
for (const captureIndice of rule.captureIndices) {
if (captureIndice.start == tokenStart && captureIndice.end >= tokenEnd) {
foundRule = true;
// Keep going if next token is identical. (((x))) => 3 not 1
}
else if (foundRule) {
break;
// Should keep going if next token is identical. (((x))) => 3 not 1
}
captureIndex++;
}
Expand All @@ -805,14 +771,17 @@ async function gotoGrammar(element: element) {
break;
}
}
if (!ruleId) {
ruleId = token.ruleId;
}
}
}

// vscode.window.showInformationMessage(JSON.stringify(ruleId));
if (!ruleId) {
return;
}

if (ruleId < 0) {
ruleId = -ruleId;
}
Expand Down Expand Up @@ -898,7 +867,6 @@ async function gotoGrammar(element: element) {

async function gotoFile(element: element) {
// vscode.window.showInformationMessage(JSON.stringify("goto"));
// vscode.window.showInformationMessage(JSON.stringify(args));

if (!element) {
return;
Expand Down Expand Up @@ -956,9 +924,10 @@ async function gotoFile(element: element) {
}
if (id == 1) {
const token = grammar.lines[line].tokens[element.tokenId];
const range = new vscode.Range(line, token.startIndex, line, token.endIndex);
const location = new vscode.Location(
document.uri,
new vscode.Range(line, token.startIndex, line, token.endIndex),
range,
);
const position = new vscode.Position(line, token.startIndex);
vscode.commands.executeCommand('editor.action.goToLocations', document.uri, position, [location]);
Expand All @@ -967,7 +936,7 @@ async function gotoFile(element: element) {
}
}

function allChildren(rules: vscodeTextmate.IRawGrammar | IRawRule, ruleId: number, captureIndex?: number): [{ 'repository'?: string, 'patterns'?: number, 'captures'?: string, id?: number }] {
function allChildren(rules: vscodeTextmate.IRawGrammar | IRawRule, ruleId: number, captureIndex?: number): [{ 'repository'?: string, 'patterns'?: number, 'captures'?: string, id?: number; }] {
for (const key in rules) {
switch (key) {
case 'patterns':
Expand Down

0 comments on commit baf548c

Please sign in to comment.