Skip to content

Commit

Permalink
Fix: check type of grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
pfe171 committed May 28, 2024
1 parent ac35a3f commit 8311a80
Showing 1 changed file with 34 additions and 32 deletions.
66 changes: 34 additions & 32 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,45 +43,47 @@ export async function activate(context: vscode.ExtensionContext) {
if (selectedText != undefined && language != undefined) {
registry
.loadGrammar('source.' + language)
.then((grammar: any) => {
.then((grammar: vscodeTextmate.IGrammar | null) => {
let ruleStack = vscodeTextmate.INITIAL;
// loop every line
for (let i = 0; i < selectedText.length; i++) {
const line = selectedText[i];
const lineTokens = grammar.tokenizeLine(line, ruleStack);
let offset = 0;
// loop every token
for (let j = 0; j < lineTokens.tokens.length; j++) {
const token = lineTokens.tokens[j];
// detect and remove comment symbols
if (
token.scopes.some((s: string) => {
return s.includes('punctuation.definition.comment');
}) ||
(token.scopes.some((s: string) => {
return s.includes('punctuation');
}) &&
if (grammar != null) {
// loop every line
for (let i = 0; i < selectedText.length; i++) {
const line = selectedText[i];
const lineTokens = grammar.tokenizeLine(line, ruleStack);
let offset = 0;
// loop every token
for (let j = 0; j < lineTokens.tokens.length; j++) {
const token = lineTokens.tokens[j];
// detect and remove comment symbols
if (
token.scopes.some((s: string) => {
return s.includes('comment.block');
}))
) {
selectedText[i] =
selectedText[i].substring(0, token.startIndex - offset) +
selectedText[i].substring(token.endIndex - offset);
offset += token.endIndex - token.startIndex;
return s.includes('punctuation.definition.comment');
}) ||
(token.scopes.some((s: string) => {
return s.includes('punctuation');
}) &&
token.scopes.some((s: string) => {
return s.includes('comment.block');
}))
) {
selectedText[i] =
selectedText[i].substring(0, token.startIndex - offset) +
selectedText[i].substring(token.endIndex - offset);
offset += token.endIndex - token.startIndex;
}
}
ruleStack = lineTokens.ruleStack;
}
ruleStack = lineTokens.ruleStack;
}

// restructure text (add new-line character)
const restructuredText = restructureText(selectedText);
// restructure text (add new-line character)
const restructuredText = restructureText(selectedText);

// copy to clipboard
vscode.env.clipboard.writeText(restructuredText);
vscode.window.showInformationMessage('Copied without comment symbols!');
// copy to clipboard
vscode.env.clipboard.writeText(restructuredText);
vscode.window.showInformationMessage('Copied without comment symbols!');
}
})
.catch((e) => vscode.window.showErrorMessage('This programming language is not supported.'));
.catch(() => vscode.window.showErrorMessage('This programming language is not supported.'));
} else {
vscode.window.showErrorMessage('Failed to get document');
}
Expand Down

0 comments on commit 8311a80

Please sign in to comment.