Skip to content

Commit 063ed96

Browse files
committed
HTML formatting is now working
1 parent e8e8aa6 commit 063ed96

10 files changed

Lines changed: 487 additions & 5674 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules
2+
test.mjs

dist/main.js

Lines changed: 240 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
{
22
"name": "acode-plugin-prettier",
3-
"version": "1.0.2",
3+
"version": "1.0.1",
44
"description": "Acode plugin to format code",
55
"main": "dist/main.js",
66
"repository": "https://github.com/deadlyjack/acode-plugin-prettier.git",
77
"author": "Ajit <me@ajitkumar.dev>",
88
"license": "MIT",
99
"dependencies": {
10-
"autoprefixer": "^10.4.7",
11-
"html-tag-js": "^1.0.4",
12-
"prettier": "^2.7.1",
13-
"sass-loader": "^13.0.0"
10+
"prettier": "^2.7.1"
1411
},
1512
"devDependencies": {
1613
"@babel/cli": "^7.17.10",
1714
"@babel/core": "^7.18.5",
1815
"@babel/preset-env": "^7.18.2",
1916
"babel-loader": "^8.2.5",
20-
"css-loader": "^6.7.1",
21-
"mini-css-extract-plugin": "^2.6.0",
22-
"node-sass": "^7.0.1",
23-
"postcss-loader": "^7.0.0",
24-
"raw-loader": "^4.0.2",
25-
"style-loader": "^3.3.1",
2617
"webpack": "^5.73.0",
2718
"webpack-cli": "^4.10.0"
2819
},
2920
"scripts": {
3021
"build": "webpack"
3122
}
32-
}
23+
}

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "acode.plugin.prettier",
33
"name": "Prettier",
44
"main": "dist/main.js",
5-
"version": "1.0.0",
5+
"version": "1.0.1",
66
"readme": "readme.md",
77
"icon": "icon.png",
88
"type": "free",

postcss.config.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

readme.md

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,33 @@
22

33
Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary.
44

5-
This plugin can formate code wrtten in [Acode](https://acode.foxdebug.com) editor for android.
5+
This plugin can format code written in [Acode](https://acode.foxdebug.com) editor for Android.
66

7-
## Plugin underdevelopment
7+
## Supported Extenstions
8+
- html
9+
- htm
10+
- css
11+
- scss
12+
- less
13+
- js
14+
- cjs
15+
- es
16+
- mjs
17+
- jsx
18+
- ts
19+
- tsx
20+
- vue
21+
- json
22+
- hbs
23+
- handlebars
24+
- md
25+
- yaml
26+
- yml
827

9-
This plugin is currently underdevelopment, it may not work properly.
28+
## Under Development
29+
30+
This plugin is currently under development so it may not work properly.
1031

1132
## Known issues
1233

13-
- Makes app stuck while formatting HTML (any markup language)
14-
- Supports only limitted languages.
15-
- js, jsx, ts, tsx, json
16-
- css, scss, less
17-
- graphQL, md, yaml
34+
None so far. If you find any issues, please raise it in GitHub issues. Thank you.

src/main.js

Lines changed: 163 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,172 @@
1-
import prettier from 'prettier/standalone';
2-
import prettierParserBabel from 'prettier/parser-babel';
3-
import prettierParserGraphql from 'prettier/parser-graphql';
4-
import prettierParserAngular from 'prettier/parser-angular';
5-
import prettierParserAspree from 'prettier/parser-espree';
6-
import prettierParserFlow from 'prettier/parser-flow';
7-
import prettierParserGlimmer from 'prettier/parser-glimmer';
8-
import prettierParserHtml from 'prettier/parser-html';
9-
import prettierParserMd from 'prettier/parser-markdown';
10-
import prettierParserMeriyah from 'prettier/parser-meriyah';
11-
import prettierParserPostcss from 'prettier/parser-postcss';
12-
import prettierParserTypescript from 'prettier/parser-typescript';
13-
import prettierParserYaml from 'prettier/parser-yaml';
14-
15-
const pluginId = 'acode.plugin.prettier';
16-
const poluginList = [
17-
prettierParserBabel,
18-
prettierParserGraphql,
19-
prettierParserAngular,
20-
prettierParserAspree,
21-
prettierParserFlow,
22-
prettierParserGlimmer,
23-
prettierParserHtml,
24-
prettierParserMd,
25-
prettierParserMeriyah,
26-
prettierParserPostcss,
27-
prettierParserTypescript,
28-
prettierParserYaml,
1+
import prettier from "prettier/standalone";
2+
import pretterParserHTML from "prettier/parser-html";
3+
import prettierParserBabel from "prettier/parser-babel";
4+
import prettierParserGraphql from "prettier/parser-graphql";
5+
import prettierParserAngular from "prettier/parser-angular";
6+
import prettierParserAspree from "prettier/parser-espree";
7+
import prettierParserFlow from "prettier/parser-flow";
8+
import prettierParserGlimmer from "prettier/parser-glimmer";
9+
import prettierParserMd from "prettier/parser-markdown";
10+
import prettierParserMeriyah from "prettier/parser-meriyah";
11+
import prettierParserPostcss from "prettier/parser-postcss";
12+
import prettierParserTypescript from "prettier/parser-typescript";
13+
import prettierParserYaml from "prettier/parser-yaml";
14+
15+
const plugins = [
16+
prettierParserBabel,
17+
prettierParserGraphql,
18+
prettierParserAngular,
19+
prettierParserAspree,
20+
prettierParserFlow,
21+
prettierParserGlimmer,
22+
pretterParserHTML,
23+
prettierParserMd,
24+
prettierParserMeriyah,
25+
prettierParserPostcss,
26+
prettierParserTypescript,
27+
prettierParserYaml,
2928
];
3029

30+
const pluginId = "acode.plugin.prettier";
31+
3132
class Prettier {
33+
static inferParser(filename) {
34+
switch (filename.slice(filename.lastIndexOf(".") + 1)) {
35+
case "html":
36+
case "htm":
37+
return "html";
3238

33-
async init() {
34-
const config = appSettings.value[pluginId];
35-
}
36-
37-
async run() {
38-
const { editor, activeFile } = editorManager;
39-
const code = editor.getValue();
40-
const cursorPos = editor.getCursorPosition();
41-
const res = prettier.formatWithCursor(code, {
42-
cursorOffset: this.#cursorPosTocursorOffset(cursorPos),
43-
filepath: activeFile.name,
44-
plugins: poluginList,
45-
});
46-
editor.setValue(res.formatted);
47-
const { row, column } = this.#cursorOffsetTocursorPos(res.cursorOffset);
48-
setTimeout(() => {
49-
editor.gotoLine(row + 1, column - 1);
50-
}, 100);
51-
}
52-
53-
destroy() {
54-
55-
}
56-
57-
#cursorPosTocursorOffset(cursorPos) {
58-
let { row, column } = cursorPos;
59-
const { editor } = editorManager;
60-
const lines = editor.getValue().split('\n');
61-
for (let i = 0; i < row - 1; i++) {
62-
column += lines[i].length;
39+
case "css":
40+
return "css";
41+
42+
case "scss":
43+
return "scss";
44+
45+
case "less":
46+
return "less";
47+
48+
case "js":
49+
case "cjs":
50+
case "es":
51+
case "mjs":
52+
case "jsx":
53+
return "babel";
54+
55+
case "ts":
56+
case "tsx":
57+
return "typescript";
58+
59+
case "vue":
60+
return "vue";
61+
62+
case "json":
63+
return "json";
64+
65+
case "hbs":
66+
case "handlebars":
67+
return "glimmer";
68+
69+
case "md":
70+
return "markdown";
71+
72+
case "yaml":
73+
case "yml":
74+
return "yaml";
75+
76+
default:
77+
return null;
78+
}
6379
}
64-
return column;
65-
}
66-
67-
#cursorOffsetTocursorPos(cursorOffset) {
68-
const { editor } = editorManager;
69-
const lines = editor.getValue().split('\n');
70-
let row = 0;
71-
let column = 0;
72-
for (let i = 0; i < lines.length; i++) {
73-
if (column + lines[i].length >= cursorOffset) {
74-
row = i;
75-
column = cursorOffset - column;
76-
break;
77-
}
78-
column += lines[i].length;
80+
81+
async init() {
82+
const config = appSettings.value[pluginId];
83+
}
84+
85+
async run() {
86+
const { editor, activeFile } = editorManager;
87+
const code = editor.getValue();
88+
const cursorPos = editor.getCursorPosition();
89+
const parser = Prettier.inferParser(activeFile.name);
90+
console.log("biraj's parser is", parser);
91+
const res = prettier.formatWithCursor(code, {
92+
parser,
93+
cursorOffset: this.#cursorPosTocursorOffset(cursorPos),
94+
filepath: activeFile.name,
95+
plugins,
96+
});
97+
editor.setValue(res.formatted);
98+
const { row, column } = this.#cursorOffsetTocursorPos(res.cursorOffset);
99+
setTimeout(() => {
100+
editor.gotoLine(row + 1, column - 1);
101+
}, 100);
102+
}
103+
104+
destroy() {}
105+
106+
#cursorPosTocursorOffset(cursorPos) {
107+
let { row, column } = cursorPos;
108+
const { editor } = editorManager;
109+
const lines = editor.getValue().split("\n");
110+
for (let i = 0; i < row - 1; i++) {
111+
column += lines[i].length;
112+
}
113+
return column;
114+
}
115+
116+
#cursorOffsetTocursorPos(cursorOffset) {
117+
const { editor } = editorManager;
118+
const lines = editor.getValue().split("\n");
119+
let row = 0;
120+
let column = 0;
121+
for (let i = 0; i < lines.length; i++) {
122+
if (column + lines[i].length >= cursorOffset) {
123+
row = i;
124+
column = cursorOffset - column;
125+
break;
126+
}
127+
column += lines[i].length;
128+
}
129+
return {
130+
row,
131+
column,
132+
};
79133
}
80-
return {
81-
row,
82-
column,
83-
};
84-
}
85134
}
86135

87136
if (window.acode) {
88-
const prettier = new Prettier();
89-
acode.setPluginInit(pluginId, (baseUrl, $page, { cacheFileUrl, cacheFile }) => {
90-
if (!baseUrl.endsWith('/')) {
91-
baseUrl += '/';
92-
}
93-
prettier.baseUrl = baseUrl;
94-
prettier.init($page, cacheFile, cacheFileUrl);
95-
console.log('Python plugin initialized');
96-
});
97-
acode.setPluginUnmount(pluginId, () => {
98-
prettier.destroy();
99-
console.log('Python plugin unmounted');
100-
});
101-
const extensions = ['js', 'jsx', 'ts', 'tsx', 'css', 'scss', 'less', 'json', 'yml', 'yaml', 'xml', 'md'];
102-
// const extensions = ['js', 'jsx', 'ts', 'tsx', 'html', 'css', 'scss', 'less', 'json', 'yml', 'yaml', 'xml', 'vue', 'hbs', 'ejs', 'md'];
103-
acode.registerFormatter(pluginId, extensions, prettier.run.bind(prettier));
104-
}
137+
const prettier = new Prettier();
138+
acode.setPluginInit(pluginId, (baseUrl, $page, { cacheFileUrl, cacheFile }) => {
139+
if (!baseUrl.endsWith("/")) {
140+
baseUrl += "/";
141+
}
142+
prettier.baseUrl = baseUrl;
143+
prettier.init($page, cacheFile, cacheFileUrl);
144+
});
145+
acode.setPluginUnmount(pluginId, () => {
146+
prettier.destroy();
147+
});
148+
149+
const extensions = [
150+
"html",
151+
"htm",
152+
"css",
153+
"scss",
154+
"less",
155+
"js",
156+
"cjs",
157+
"es",
158+
"mjs",
159+
"jsx",
160+
"ts",
161+
"tsx",
162+
"vue",
163+
"json",
164+
"hbs",
165+
"handlebars",
166+
"md",
167+
"yaml",
168+
"yml",
169+
];
170+
171+
acode.registerFormatter(pluginId, extensions, prettier.run.bind(prettier));
172+
}

0 commit comments

Comments
 (0)