Skip to content

Commit 80af2a2

Browse files
authored
Merge pull request #28 from williamthome/refactor/lint
refactor: lint
2 parents 8f6d103 + fc96e54 commit 80af2a2

File tree

10 files changed

+136
-150
lines changed

10 files changed

+136
-150
lines changed

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "zotonic-ls",
33
"displayName": "Zotonic",
44
"description": "Syntax highlight for Zotonic templates (.tpl).",
5-
"version": "0.4.0",
5+
"version": "0.4.1",
66
"publisher": "williamthome",
77
"license": "Apache-2.0",
88
"icon": "images/z_logo.png",
@@ -36,7 +36,12 @@
3636
"definitionProvider": "true",
3737
"hoverProvider": "true",
3838
"completionProvider": {
39-
"triggerCharacters": [ ".", "[", "{", "|" ]
39+
"triggerCharacters": [
40+
".",
41+
"[",
42+
"{",
43+
"|"
44+
]
4045
}
4146
},
4247
"main": "./out/extension.js",
@@ -141,4 +146,4 @@
141146
"dependencies": {
142147
"axios": "^0.27.2"
143148
}
144-
}
149+
}

src/config/docs.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Docs } from "../utils/docs"
1+
import { Docs } from "../utils/docs";
22

33
export type DocsConfig = {
44
docs: Docs,
55
language: string,
66
version: string,
77
branch: string,
8-
}
8+
};
99

1010
const docsConfig: DocsConfig = {
1111
docs: {
@@ -464,6 +464,6 @@ const docsConfig: DocsConfig = {
464464
language: "en",
465465
version: "latest",
466466
branch: "master",
467-
}
467+
};
468468

469-
export default docsConfig
469+
export default docsConfig;

src/config/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import getDoc from "../utils/docs"
2-
import docsConfig, { DocsConfig } from "./docs"
1+
import getDoc from "../utils/docs";
2+
import docsConfig, { DocsConfig } from "./docs";
33

44
type Config = {
55
docs: DocsConfig,
6-
}
6+
};
77

88
const config: Config = {
99
docs: docsConfig
10-
}
10+
};
1111

1212
export default {
1313
defaults: config,
@@ -17,4 +17,4 @@ export default {
1717
docsConfig.version,
1818
docsConfig.branch
1919
)
20-
}
20+
};

src/extension.ts

Lines changed: 74 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import * as vscode from 'vscode';
44
import axios from "axios";
55
import config from "./config";
6-
import { m_get, Expression, FindFile } from './utils/snippets';
6+
import { mGetExpressions, Expression, FindFile } from './utils/snippets';
77

88
// this method is called when your extension is activated
99
// your extension is activated the very first time the command is executed
@@ -21,54 +21,54 @@ export function activate(context: vscode.ExtensionContext) {
2121
// @see this question in StackOverflow here https://stackoverflow.com/questions/72554515/go-to-definition-highlight-in-vscode-lsp
2222
const definitionProvider = vscode.languages.registerDefinitionProvider('tpl', {
2323
async provideDefinition(document, position, _token) {
24-
const wordRange = document.getWordRangeAtPosition(position, /(?<=").*?(?=")/)
25-
if (!wordRange || wordRange.isEmpty) return
24+
const wordRange = document.getWordRangeAtPosition(position, /(?<=").*?(?=")/);
25+
if (!wordRange || wordRange.isEmpty) { return; };
2626

27-
const text = document.getText(wordRange)
28-
const lastDotIndex = text.lastIndexOf(".")
29-
if (lastDotIndex <= 0) return
27+
const text = document.getText(wordRange);
28+
const lastDotIndex = text.lastIndexOf(".");
29+
if (lastDotIndex <= 0) { return; };
3030

31-
const ext = text.substring(lastDotIndex)
32-
if (!ext) return
31+
const ext = text.substring(lastDotIndex + 1);
32+
if (!ext) { return; };
3333

34-
const lastSlashIndex = text.lastIndexOf("/")
35-
const fileName = text.substring(lastSlashIndex).replace("/", "")
36-
if (!fileName) return
34+
const lastSlashIndex = text.lastIndexOf("/");
35+
const fileName = text.substring(lastSlashIndex).replace("/", "");
36+
if (!fileName) { return; };
3737

38-
const tplLocationPattern = "**/priv/templates/**/"
39-
const jsLocationPattern = "**/priv/lib/**/"
40-
const cssLocationPattern = "**/priv/lib/**/"
41-
const imageLocationPattern = "**/priv/lib/images/**/"
38+
const tplLocationPattern = "**/priv/templates/**/";
39+
const jsLocationPattern = "**/priv/lib/**/";
40+
const cssLocationPattern = "**/priv/lib/**/";
41+
const imageLocationPattern = "**/priv/lib/images/**/";
4242

4343
const extLocationPattern = {
44-
".tpl": tplLocationPattern,
45-
".js": jsLocationPattern,
46-
".css": cssLocationPattern,
47-
".apng": imageLocationPattern,
48-
".gif": imageLocationPattern,
49-
".ico": imageLocationPattern,
50-
".cur": imageLocationPattern,
51-
".jpg": imageLocationPattern,
52-
".jpeg": imageLocationPattern,
53-
".jfif": imageLocationPattern,
54-
".pjpeg": imageLocationPattern,
55-
".pjp": imageLocationPattern,
56-
".png": imageLocationPattern,
57-
".svg": imageLocationPattern,
58-
}
59-
60-
const locationPattern = extLocationPattern[ext as keyof typeof extLocationPattern]
61-
if (!locationPattern) return
44+
tpl: tplLocationPattern,
45+
js: jsLocationPattern,
46+
css: cssLocationPattern,
47+
apng: imageLocationPattern,
48+
gif: imageLocationPattern,
49+
ico: imageLocationPattern,
50+
cur: imageLocationPattern,
51+
jpg: imageLocationPattern,
52+
jpeg: imageLocationPattern,
53+
jfif: imageLocationPattern,
54+
pjpeg: imageLocationPattern,
55+
pjp: imageLocationPattern,
56+
png: imageLocationPattern,
57+
svg: imageLocationPattern,
58+
};
59+
60+
const locationPattern = extLocationPattern[ext as keyof typeof extLocationPattern];
61+
if (!locationPattern) { return; };
6262

6363
const filePattern = `${locationPattern}${fileName}`;
64-
const ignorePattern = "**/_build/**"
64+
const ignorePattern = "**/_build/**";
6565

6666
const files = await vscode.workspace.findFiles(filePattern, ignorePattern);
67-
if (!files) return;
67+
if (!files) { return; }
6868

6969
return files.map(uri => new vscode.Location(
7070
uri, new vscode.Position(0, 0)
71-
))
71+
));
7272
}
7373
});
7474

@@ -77,17 +77,17 @@ export function activate(context: vscode.ExtensionContext) {
7777
const hoverProvider = vscode.languages.registerHoverProvider('tpl', {
7878
async provideHover(document, position, _token) {
7979
const patternMatch = (pattern: RegExp) => {
80-
const wordRange = document.getWordRangeAtPosition(position, pattern)
80+
const wordRange = document.getWordRangeAtPosition(position, pattern);
8181
return !!wordRange && !wordRange.isEmpty
8282
? document.getText(wordRange)
83-
: undefined
84-
}
83+
: undefined;
84+
};
8585

86-
const doc = config.getDoc(patternMatch)
87-
if (!doc || !doc.raw) return
86+
const doc = config.getDoc(patternMatch);
87+
if (!doc || !doc.raw) { return; };
8888

8989
const response = await axios.get(doc.raw);
90-
if (response.status !== 200) return
90+
if (response.status !== 200) { return; };
9191

9292
const markdown = new vscode.MarkdownString(response.data);
9393
markdown.supportHtml = true;
@@ -107,78 +107,77 @@ export function activate(context: vscode.ExtensionContext) {
107107
_token: vscode.CancellationToken,
108108
_context: vscode.CompletionContext
109109
) {
110-
const modelNameRe = /\bm\.(\w+)?/
111-
const modelNameRange = document.getWordRangeAtPosition(position, modelNameRe)
110+
const modelNameRe = /\bm\.(\w+)?/;
111+
const modelNameRange = document.getWordRangeAtPosition(position, modelNameRe);
112112
if (!!modelNameRange && !modelNameRange.isEmpty) {
113-
const modelsPattern = "{apps,apps_user}/**/src/models/**/m_*.erl"
113+
const modelsPattern = "{apps,apps_user}/**/src/models/**/m_*.erl";
114114
const models = await vscode.workspace.findFiles(modelsPattern);
115115
return models.reduce((arr, file) => {
116-
const modelRe = /(?<=\bm_).*?(?=.erl)/
117-
const modelMatch = modelRe.exec(file.fsPath)
116+
const modelRe = /(?<=\bm_).*?(?=.erl)/;
117+
const modelMatch = modelRe.exec(file.fsPath);
118118
if (!modelMatch || !modelMatch.length) {
119-
return arr
119+
return arr;
120120
}
121121

122-
const model = modelMatch[0]
123-
const modelExpressionsFinder = (m: string) => m_get(findFile, m)
124-
const snippet = new vscode.CompletionItem(model)
125-
snippet.insertText = new vscode.SnippetString(model)
122+
const model = modelMatch[0];
123+
const modelExpressionsFinder = (m: string) => mGetExpressions(findFile, m);
124+
const snippet = new vscode.CompletionItem(model);
125+
snippet.insertText = new vscode.SnippetString(model);
126126
snippet.command = {
127127
command: "tpl.snippet.pick",
128128
title: "m_get",
129129
arguments: [model, modelExpressionsFinder]
130-
}
131-
arr.push(snippet)
132-
return arr
133-
}, new Array<vscode.CompletionItem>())
130+
};
131+
arr.push(snippet);
132+
return arr;
133+
}, new Array<vscode.CompletionItem>());
134134
}
135135

136-
const variableRe = /(?<=\[).*?(?=\])|(?<={).*?(?=})|(?<=:).*?(?=}|,)|(?<==).*?(?=(}|,|%}))/
137-
const variableRange = document.getWordRangeAtPosition(position, variableRe)
136+
const variableRe = /(?<=\[).*?(?=\])|(?<={).*?(?=})|(?<=:).*?(?=}|,)|(?<==).*?(?=(}|,|%}))/;
137+
const variableRange = document.getWordRangeAtPosition(position, variableRe);
138138
if (!!variableRange) {
139139
// TODO: Variables snippets.
140140
// It will be awesome if variables can pop up as suggestion.
141-
return
141+
return;
142142
}
143143

144-
const mSnippet = new vscode.CompletionItem("m")
145-
mSnippet.insertText = new vscode.SnippetString("m")
144+
const mSnippet = new vscode.CompletionItem("m");
145+
mSnippet.insertText = new vscode.SnippetString("m");
146146

147147
return [
148148
mSnippet
149-
]
149+
];
150150
}
151-
// })
152-
}, ".", "[", "{", "|")
151+
}, ".", "[", "{", "|");
153152

154-
context.subscriptions.push(completionProvider)
153+
context.subscriptions.push(completionProvider);
155154

156155
vscode.commands.registerCommand('tpl.snippet.pick', async (model, modelExpressionsFinder) => {
157-
const expressions: Array<Expression> = await modelExpressionsFinder(model)
156+
const expressions: Array<Expression> = await modelExpressionsFinder(model);
158157
if (expressions instanceof Error) {
159-
await vscode.window.showErrorMessage(expressions.message)
160-
return undefined
158+
await vscode.window.showErrorMessage(expressions.message);
159+
return undefined;
161160
}
162161

163162
const quickPick = vscode.window.createQuickPick();
164163
quickPick.items = expressions.map(({ expression: label }) => ({ label }));
165164
quickPick.onDidChangeSelection(async ([{ label }]) => {
166-
const token = expressions.find(token => token.expression === label)
165+
const token = expressions.find(token => token.expression === label);
167166
if (!token) {
168-
throw (new Error(`Unexpected no token match in quick pick with label '${label}'`))
167+
throw (new Error(`Unexpected no token match in quick pick with label '${label}'`));
169168
}
170169

171-
await vscode.commands.executeCommand("tpl.snippet.insert", token.snippet)
170+
await vscode.commands.executeCommand("tpl.snippet.insert", token.snippet);
172171
quickPick.hide();
173172
});
174173
quickPick.show();
175-
})
174+
});
176175

177176
vscode.commands.registerTextEditorCommand('tpl.snippet.insert', (editor, _edit, snippet) => {
178177
return editor.insertSnippet(
179178
new vscode.SnippetString(snippet),
180179
);
181-
})
180+
});
182181
}
183182

184183
// this method is called when your extension is deactivated
@@ -190,5 +189,5 @@ const findFile: FindFile = async (pattern, ignorePattern = null) => {
190189
const files = await vscode.workspace.findFiles(pattern, ignorePattern, 1);
191190
return files.length
192191
? files[0].fsPath
193-
: undefined
194-
}
192+
: undefined;
193+
};
File renamed without changes.

src/utils/docs.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,40 @@ export type Doc = {
44
pattern: RegExp,
55
genUri: (token: string) => string,
66
tokens?: string[]
7-
}
7+
};
88

9-
export type Docs = { [key: string]: Doc }
9+
export type Docs = { [key: string]: Doc };
1010

1111
// API
1212

1313
export default function (docs: Docs, language: string, version: string, branch: string) {
1414
return function (patternMatch: (pattern: RegExp) => string | undefined) {
15-
const uri = maybeGenDocUri(docs, patternMatch)
15+
const uri = maybeGenDocUri(docs, patternMatch);
1616
return {
1717
uri: maybeGenDocUri(docs, patternMatch),
1818
url: uri ? genDocUrl(language, version, uri) : undefined,
1919
raw: uri ? genDocRawUrl(branch, uri) : undefined,
20-
}
21-
}
20+
};
21+
};
2222
}
2323

2424
// Internal functions
2525

2626
function genDocUrl(language: string, version: string, uri = "/") {
27-
return `http://docs.zotonic.com/${language}/${version}${uri}`
27+
return `http://docs.zotonic.com/${language}/${version}${uri}`;
2828
}
2929

3030
function genDocRawUrl(branch: string, uri: string) {
31-
return `https://raw.githubusercontent.com/zotonic/zotonic/${branch}/doc${uri}.rst`
31+
return `https://raw.githubusercontent.com/zotonic/zotonic/${branch}/doc${uri}.rst`;
3232
}
3333

3434
function maybeGenDocUri(
3535
docs: Docs,
3636
patternMatch: (pattern: RegExp) => string | undefined
3737
) {
3838
for (const doc of Object.values(docs)) {
39-
const token = patternMatch(doc.pattern)
40-
if (!token || (doc.tokens?.length && !doc.tokens.includes(token))) continue
41-
return doc.genUri(token)
39+
const token = patternMatch(doc.pattern);
40+
if (!token || (doc.tokens?.length && !doc.tokens.includes(token))) { continue; };
41+
return doc.genUri(token);
4242
}
4343
}

0 commit comments

Comments
 (0)