Skip to content

Commit

Permalink
Goto line doc (#26)
Browse files Browse the repository at this point in the history
* Iniciado implementação do Go-Line Doc

* Fix #16
  • Loading branch information
AlencarGabriel authored May 14, 2020
1 parent 7be63de commit 6556616
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
44 changes: 42 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ export function activate(context: vscode.ExtensionContext) {

// register the additional command (not really necessary, unless you want a command registered in your extension)
context.subscriptions.push(vscode.commands.registerCommand("protheusdoc.whatsNew", () => viewer.showPage()));

// Registra o comando que abrirá o arquivo na linha da documentação
context.subscriptions.push(vscode.commands.registerCommand("protheusdoc.openFile", (args) => {
vscode.window.showTextDocument(vscode.Uri.parse(args.file)).then(textEditor => {
let range = textEditor.document.lineAt(args.line).range;
textEditor.selection = new vscode.Selection(range.start, range.start);
textEditor.revealRange(range);
});
}));

// Atualiza tabela de documentações do Workspace
searchProtheusDoc();
Expand All @@ -129,20 +138,51 @@ export function searchProtheusDocInFile(text: string, uri: vscode.Uri) {
let expressionProtheusDoc = /(\{Protheus\.doc\}\s*)([^*]*)(\n[^:\n]*)/mig;
let match = text.match(expressionProtheusDoc);

/**
* Limpa o texto antes de montar uma expressão regular.
* @param string texto a ser limpo.
*/
function escapeRegExp(string:string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

/**
* Busca o numero da linha que contém o identificador.
* @param identificador identificador do ProtheusDoc.
*/
function findLine(identificador: string): number {
let expressionProtheusDoc2 = new RegExp("(\\{Protheus\\.doc\\}\\s*)(" + escapeRegExp(identificador.trim()) + ")", "i");
let texts = text.split("\n");

// Percorre o array das linhas para verificar onde está a declaração do identificador
for (let line = 0; line < texts.length; line++) {
let match = texts[line].match(expressionProtheusDoc2);

if (match !== null && match.index !== undefined) {
return line;
}
}

return 0;
}

// Remove todas as referências de documentação do arquivo aberto
documentations = documentations.filter(doc => doc.file.fsPath !== uri.fsPath);

if (match) {
// Percorre via expressão regular todas as ocorrencias de ProtheusDoc no arquivo.
match.forEach(element => {
let doc = new ProtheusDocToDoc(element, uri).getDocumentation();
let doc = new ProtheusDocToDoc(element, uri);
let docIndex = documentations.findIndex(e => e.identifier.trim().toUpperCase() === doc.identifier.trim().toUpperCase());

// Adiciona a linha correspondente a documentação
doc.lineNumber = findLine(doc.identifier);

// Caso a documentação já exista na lista altera
// if (docIndex >= 0) {
// documentations[docIndex] = doc;
// } else {
documentations.push(doc);
documentations.push(doc.getDocumentation());
// }
});
}
Expand Down
12 changes: 11 additions & 1 deletion src/objects/Documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class Documentation {
public params: IParam[];
public return: IParam;
public file: vscode.Uri;
public line: number;

constructor(documentation: ProtheusDocToDoc) {
this.identifier = documentation.identifier;
Expand All @@ -31,13 +32,17 @@ export class Documentation {
this.params = documentation.params;
this.return = documentation.return;
this.file = documentation.file;
this.line = documentation.lineNumber;
}

/**
* Retotorna a documentação a ser exibida no Hover.
*/
public getHover(): vscode.MarkdownString {
let doc = new vscode.MarkdownString;

// To enable command URIs in Markdown content, you must set the `isTrusted` flag.
doc.isTrusted = true;

if (this.type.trim().toUpperCase() === ETypesDoc.function.toString().toUpperCase()) {
doc.appendCodeblock("Function " + this.identifier.trim() + "()", "advpl");
Expand Down Expand Up @@ -66,8 +71,11 @@ export class Documentation {
doc.appendMarkdown("\r\n *@return* " + this.return.paramDescription.trim() + "\r\n");
}

// Aciona o documento com link para a documentação do arquivo
let dirs = this.file.fsPath.toString().split(path.sep);
doc.appendMarkdown("\r\n **Localização**: [" + dirs[dirs.length - 2] + path.sep + dirs[dirs.length - 1] + "](" + this.file.toString() + ") \r\n");
const args = [{ file: this.file.toString(), line: this.line }];
const link = vscode.Uri.parse(`command:protheusdoc.openFile?${encodeURIComponent(JSON.stringify(args))}`);
doc.appendMarkdown("\r\n **Localização**: [" + dirs[dirs.length - 2] + path.sep + dirs[dirs.length - 1] + "](" + `${link}` + ") \r\n");

return doc;
}
Expand All @@ -89,6 +97,7 @@ export class ProtheusDocToDoc {
public params: IParam[];
public return: IParam;
public file: vscode.Uri;
public lineNumber: number;

constructor(protheusDocBlock: string, file: vscode.Uri) {
this._protheusDocBlock = protheusDocBlock;
Expand All @@ -103,6 +112,7 @@ export class ProtheusDocToDoc {
this.params = [];
this.return = { paramName: "", paramType: "", paramDescription: "" };
this.file = file;
this.lineNumber = 0;

this.toBreak();
}
Expand Down

0 comments on commit 6556616

Please sign in to comment.