Skip to content

Commit cfb79fa

Browse files
committed
first commit
0 parents  commit cfb79fa

13 files changed

Lines changed: 7972 additions & 0 deletions

.babelrc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env"
4+
],
5+
"env": {
6+
"test": {
7+
"presets": [
8+
"@babel/env"
9+
]
10+
}
11+
},
12+
"compact": false
13+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

dist/main.js

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

dist/main.js.LICENSE.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */

icon.png

1.39 KB
Loading

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "acode-plugin-prettier",
3+
"version": "1.0.2",
4+
"description": "Acode plugin to format code",
5+
"main": "dist/main.js",
6+
"repository": "https://github.com/deadlyjack/acode-plugin-prettier.git",
7+
"author": "Ajit <me@ajitkumar.dev>",
8+
"license": "MIT",
9+
"dependencies": {
10+
"autoprefixer": "^10.4.7",
11+
"html-tag-js": "^1.0.4",
12+
"prettier": "^2.7.1",
13+
"sass-loader": "^13.0.0"
14+
},
15+
"devDependencies": {
16+
"@babel/cli": "^7.17.10",
17+
"@babel/core": "^7.18.5",
18+
"@babel/preset-env": "^7.18.2",
19+
"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",
26+
"webpack": "^5.73.0",
27+
"webpack-cli": "^4.10.0"
28+
},
29+
"scripts": {
30+
"build": "webpack"
31+
}
32+
}

plugin.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"id": "acode.plugin.prettier",
3+
"name": "Prettier",
4+
"main": "dist/main.js",
5+
"version": "1.0.0",
6+
"readme": "README.md",
7+
"icon": "icon.png",
8+
"type": "free",
9+
"files": [],
10+
"author": {
11+
"name": "Ajit Kumar",
12+
"email": "me@ajitkumar.dev",
13+
"github": "deadlyjack"
14+
}
15+
}

postcss.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
plugins: [
3+
require('autoprefixer')({})
4+
]
5+
};

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# acode-plugin-perttier
2+
3+
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.
4+
5+
This plugin can formate code wrtten in [Acode](https://acode.foxdebug.com) editor for android.

src/main.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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,
29+
];
30+
31+
class Prettier {
32+
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;
63+
}
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;
79+
}
80+
return {
81+
row,
82+
column,
83+
};
84+
}
85+
}
86+
87+
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', 'html', 'css', 'scss', 'less', 'json', 'yml', 'yaml', 'xml', 'vue', 'hbs', 'ejs', 'md'];
102+
acode.registerFormatter(pluginId, extensions, prettier.run.bind(prettier));
103+
}

0 commit comments

Comments
 (0)