Skip to content

Commit

Permalink
0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
wch1618 committed Feb 17, 2022
1 parent b2606e3 commit 6c752a4
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 54 deletions.
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "dartmscodestyle",
"description": "make dart code style like ms",
"publisher": "zerolook",
"version": "0.0.1",
"version": "0.0.2",
"engines": {
"vscode": "^1.62.0"
},
Expand All @@ -22,13 +22,22 @@
{
"command": "dartmscodestyle.addcodestylecomment",
"title": "增加代码格式化辅助注释"
},
{
"command": "dartmscodestyle.deleteAllLineComment",
"title": "删除所有行开头为'//'的注释"
}
],
"menus": {
"editor/context": [
{
"command": "dartmscodestyle.addcodestylecomment",
"when":"resourceLangId == dart && editorTextFocus && !editorReadonly && !inCompositeEditor"
},
{
"command": "dartmscodestyle.deleteAllLineComment",
"when":"resourceLangId == dart && editorTextFocus && !editorReadonly && !inCompositeEditor",
"group": "9_cutcopypaste"
}
]
},
Expand Down
258 changes: 205 additions & 53 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/* eslint-disable curly */

import { createCipheriv } from 'crypto';
import path = require('path');
import * as vscode from 'vscode';
import { DocumentSemanticTokensProvider } from './semantic_provider';

//export const dartCodeExtensionIdentifier = "Dart-Code.dart-code";

export async function activate(context: vscode.ExtensionContext) {

let subs = context.subscriptions;

//console.log('Congratulations, your extension "dartexformatter" is now active!');
console.log('Congratulations, your extension "dartexformatter" is now active!');

// const dartExt = vscode.extensions.getExtension(dartCodeExtensionIdentifier);
// if (!dartExt) {
Expand All @@ -27,23 +27,30 @@ export async function activate(context: vscode.ExtensionContext) {

let te = vscode.workspace.onDidChangeTextDocument(async (ev) => {
if (ev.reason) return; //修复撤销问题
let fn = ev.document.fileName;

const doc = ev.document;

let fn = doc.fileName;
if (path.extname(fn) !== ".dart") return;

let beSetLines: number[] = [];
let map = new Map<number, number>();

let beAddLines: TData[] = [];
//console.log('reason:' + ev.reason);
ev.contentChanges.forEach(cc => {
let line = ev.document.lineAt(ev.document.positionAt(cc.rangeOffset));
if (checkNeedAddAssistComment(line)) {
beSetLines.push(line.lineNumber);
}
});
for (let i = 0; i < ev.contentChanges.length; i++) {
let cc = ev.contentChanges[i];
let line = doc.lineAt(doc.positionAt(cc.rangeOffset));

checkAddLine(doc, line, beAddLines, map);

if (beSetLines.length > 0) {
}


if (beAddLines.length > 0) {
new Promise((so, re) => {
let editor = vscode.window.visibleTextEditors.find(v => v.document.fileName === fn);
if (editor) {
addMsCodeStyleAssistComment(editor, beSetLines);
addMsCodeStyleAssistComment(editor, beAddLines);
}
});
}
Expand Down Expand Up @@ -74,6 +81,28 @@ export async function activate(context: vscode.ExtensionContext) {

subs.push(disposable);

disposable = vscode.commands.registerCommand('dartmscodestyle.deleteAllLineComment', (...args) => {
//console.log(args);
if (args && args[0] instanceof vscode.Uri) {
let uri = args[0] as vscode.Uri;
let uristr = uri.toString();
let editor = vscode.window.visibleTextEditors.find(v => v.document.uri.toString() === uristr);
if (editor) {
deleteAllLineComment(editor);
}

}
else {
if (vscode.window.activeTextEditor && vscode.window.activeTextEditor!.document.uri
&& path.extname(vscode.window.activeTextEditor!.document.uri.fsPath).toLowerCase() === ".dart") {
deleteAllLineComment(vscode.window.activeTextEditor);
}
}

});

subs.push(disposable);

//以下功能有问题
// const semanProvider = new DocumentSemanticTokensProvider();

Expand All @@ -87,51 +116,37 @@ export function deactivate() { }

//while|for|if|else|try|catch|final|do|while|class|extension

function checkNeedAddAssistComment(line: vscode.TextLine) {
function checkNeedAddAssistComment(doc: vscode.TextDocument, line: vscode.TextLine) {
const text = line.text;
return (line.firstNonWhitespaceCharacterIndex < 4
|| /\b(class|extension|abstract)\b/.test(line.text))
&& text.charAt(text.length - 1) === '{' //行尾为{
&& text.length - line.firstNonWhitespaceCharacterIndex > 1 //长度2 以上
&& !/\s*\/\//.test(text) //不能是注释//开头
&& !/\/\s*{$/.test(text) //不能是/ {结尾,排除准备加注释时只打了一个/的问题
&& text.indexOf('=') < 0
;
let b = text.length - line.firstNonWhitespaceCharacterIndex > 1 //长度2 以上
&& /[{]\s*$/.test(text) //行尾为{
&& !/\/\//.test(text) //不能是行注释
&& !/\/\s*[{]\s*$/.test(text) //不能是/ {结尾,排除准备加注释时只打了一个/的问题
&& (
isTryFinallyStart(text) || isFunctionLike(doc, line) || isClassBlock(text)
);

return b;
}

class TData {
constructor(public position: vscode.Position,
public line: vscode.TextLine,
) {

function addMsCodeStyleAssistComment(editor: vscode.TextEditor, beSetLines: number[]) {
let doc = editor.document;
editor.edit(cb => {

beSetLines.forEach(e => {
let line = doc.lineAt(e);

const text = line.text;
if (text.charAt(text.length - 1) === '{' //行尾为{
&& text.length > 1) {

let lastSecondChar = text.charAt(text.length - 2);
if (/\b(class|extension|abstract)\b/ig.test(text)
|| line.firstNonWhitespaceCharacterIndex < 2) {
cb.insert(new vscode.Position(line.lineNumber, text.length - 1)
, (lastSecondChar === " " ? "" : " ") + "//\n" + text.substring(0, line.firstNonWhitespaceCharacterIndex));
}
else if (line.firstNonWhitespaceCharacterIndex < 4) {
}
}

let isClass = true;
if(text.indexOf('=') >= 0){
isClass = false;
}
function addMsCodeStyleAssistComment(editor: vscode.TextEditor, beInsert: TData[]) {

if (isClass) {
cb.insert(new vscode.Position(line.lineNumber, line.text.length - 1)
, (lastSecondChar === " " ? "" : " ") + "//\n" + line.text.substring(0, line.firstNonWhitespaceCharacterIndex));
}
}
}
editor.edit(cb => {

beInsert.forEach(e => {
let line = e.line;
cb.insert(e.position
, "//\n" + line.text.substring(0, line.firstNonWhitespaceCharacterIndex));
});

});
}

Expand All @@ -140,13 +155,150 @@ function addDartMsCodeStyleAssistComment(editor: vscode.TextEditor) {

let doc = editor.document;
let lineCount = doc.lineCount;
let beSetLines: number[] = [];
let beAddLines: TData[] = [];
let map = new Map<number, number>();
for (let i = 0; i < lineCount; i++) {
let line = doc.lineAt(i);

checkAddLine(doc, line, beAddLines, map);
}

addMsCodeStyleAssistComment(editor, beAddLines);
}

function deleteAllLineComment(editor: vscode.TextEditor) {

let doc = editor.document;
let lineCount = doc.lineCount;
let beDeleteLines: vscode.Range[] = [];
for (let i = 0; i < lineCount; i++) {
let line = doc.lineAt(i);
if (checkNeedAddAssistComment(line)) {
beSetLines.push(i);
if (/^\s*\/\//.test(line.text)) {
beDeleteLines.push(new vscode.Range(new vscode.Position(line.lineNumber, 0), new vscode.Position(line.lineNumber + 1, 0)));
}
}

if (beDeleteLines.length > 0) {

editor.edit(cb => {

beDeleteLines.forEach(e => {
cb.delete(e);
});

});
}

}

//////
//是否为(try|finally){
function isTryFinallyStart(text: string) {
return /\b(try|finally)\b/ig.test(text);
}
//
function isFunctionLike(doc: vscode.TextDocument, line: vscode.TextLine): boolean {

let text = line.text;
let pos = text.length;
let linePos = line.lineNumber;
if (/[)]\s*[{]\s*$/.test(text)) {
for (let i = text.length - 2; i >= line.firstNonWhitespaceCharacterIndex; i--) {
if (text[i] === ")") {
pos = i;
break;
}
}

if (pos < text.length) { //存在)
let count = 0;
let src = pos;
let b = false;
for (let i = pos - 1; i >= line.firstNonWhitespaceCharacterIndex; i--) {
if (text[i] === "(") {
if (count > 0) { count--; }
else {
pos = i;
b = true;
break;
}
} else if (text[i] === ")") count++;
}

if (b) { //存在对称(

} else { //当前行不存在(
for (let p = linePos - 1; p >= 0; p--) {
let tline = doc.lineAt(p);
for (let k = tline.text.length - 1; k >= tline.firstNonWhitespaceCharacterIndex; k--) {
if (tline.text[k] === "(") {
if (count > 0) { count--; }
else {
pos = k;
b = true;
linePos = p;
break;
}
} else if (tline.text[k] === ")") count++;
}
if (b) break;
}
}

if (b) {
if (pos === 0) {
linePos--;
if (linePos >= 0) {
pos = doc.lineAt(linePos).text.length;
}
}
if (linePos >= 0 && /[a-zA-Z_$][a-zA-Z_0-9$]*$/.test(doc.lineAt(linePos).text.substring(0, pos))) {
return true;
}
}
}
}
addMsCodeStyleAssistComment(editor, beSetLines);
return false;
}

function isClassBlock(text: string): boolean {
//class aa extends ......
//......{
//对于以上分行的无能为力,涉及到语义的问题

return /\b(class|extension|abstract)\b/ig.test(text);
}
/**
* 查找需要增加行的on|catch|finally|while|else
* @param line
* @returns
*/
function findAddLinePosition(line: vscode.TextLine): Array<TData> {

let arr: TData[] = [];
//
const text = line.text;
var r = /[}]\s*(on|catch|finally|while|else)/ig;
for (let a of text.matchAll(r)) {
arr.push(new TData(new vscode.Position(line.lineNumber, a.index! + 1), line));
}
//.forEach((m, i) => m.forEach((v, j) => console.log(`group ${i},${j} : ${v}`)));

return arr;
}

function checkAddLine(doc: vscode.TextDocument, line: vscode.TextLine, beAddLines: TData[], map: Map<number, number>) {

if (map.has(line.lineNumber)) return false;

if (checkNeedAddAssistComment(doc, line)) {
beAddLines.push(new TData(new vscode.Position(line.lineNumber, line.text.length - 1), line));
map.set(line.lineNumber, line.lineNumber);
}
let pps = findAddLinePosition(line);
if (pps.length > 0) {
beAddLines.push(...pps);
}

}
//////

0 comments on commit 6c752a4

Please sign in to comment.