Skip to content

Commit

Permalink
set default d.ts format code settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mkslanc committed Apr 18, 2024
1 parent b3db1c3 commit ed40c51
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
1 change: 1 addition & 0 deletions ace-ext.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ declare module "ace-code/src/ext/modelist" {
*/
supportsFile(filename: string): RegExpMatchArray;
}

export {};
}
declare module "ace-code/src/ext/themelist" {
Expand Down
1 change: 1 addition & 0 deletions ace-lib.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ declare module "ace-code/src/lib/app_config" {
reportError: (msg: any, data: any) => void;
}
function warn(message: any, ...args: any[]): void;

export {};
namespace Ace {
type EventEmitter<T> = import("ace-code").Ace.EventEmitter<T>;
Expand Down
3 changes: 3 additions & 0 deletions ace-modules.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ declare module "ace-code/src/layer/font_metrics" {
els: any[] | HTMLElement | Text;
transformCoordinates(clientPos: any, elPos: any): any[];
}

export {};
namespace Ace {
type EventEmitter<T> = import("ace-code").Ace.EventEmitter<T>;
Expand Down Expand Up @@ -2282,6 +2283,7 @@ declare module "ace-code/src/tooltip" {
*/
doPopupsOverlap(popupA: Tooltip, popupB: Tooltip): boolean;
}

export {};
export interface HoverTooltip {
row: number;
Expand Down Expand Up @@ -6178,6 +6180,7 @@ declare module "ace-code/src/range" {
* @returns {Number}
*/
function comparePoints(p1: Point, p2: Point): number;

export {};
namespace Ace {
type Point = import("ace-code").Ace.Point;
Expand Down
10 changes: 5 additions & 5 deletions ace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,9 +599,9 @@ declare module "ace-code" {
clazz: string;
inFront?: boolean;
id?: number;
update?: (html: string[],
// TODO maybe define Marker class
marker: any, session: EditSession, config: any) => void;
update?: (html: string[],
// TODO maybe define Marker class
marker: any, session: EditSession, config: any) => void;
[key: string]: any;
}
type MarkerRenderer = (html: string[], range: Range, left: number, top: number, config: any) => void;
Expand Down Expand Up @@ -672,7 +672,7 @@ declare module "ace-code" {
}
interface SyntaxMode {
HighlightRules: {
new (config: any): HighlightRules;
new(config: any): HighlightRules;
}; //TODO: fix this
foldingRules?: FoldMode;
/**
Expand Down Expand Up @@ -734,7 +734,7 @@ declare module "ace-code" {
isBackwards: boolean;
}
var Selection: {
new (session: EditSession): Selection;
new(session: EditSession): Selection;
};
interface TextInput {
resetSelection(): void;
Expand Down
79 changes: 74 additions & 5 deletions tool/ace_declaration_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ const path = require("path");

const SEPARATE_MODULES = ["ext", "theme", "snippets", "lib"]; // adjust this list for more granularity

const defaultFormatCodeSettings = {
baseIndentSize: 0,
indentSize: 4,
tabSize: 4,
indentStyle: ts.IndentStyle.Smart,
newLineCharacter: "\n",
convertTabsToSpaces: true,
insertSpaceAfterCommaDelimiter: true,
insertSpaceAfterSemicolonInForStatements: true,
insertSpaceBeforeAndAfterBinaryOperators: true,
insertSpaceAfterConstructor: false,
insertSpaceAfterKeywordsInControlFlowStatements: true,
insertSpaceAfterFunctionKeywordForAnonymousFunctions: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true,
insertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
insertSpaceAfterOpeningAndBeforeClosingJsxExpressionBraces: false,
insertSpaceAfterTypeAssertion: false,
insertSpaceBeforeFunctionParenthesis: false,
placeOpenBraceOnNewLineForFunctions: false,
placeOpenBraceOnNewLineForControlBlocks: false,
insertSpaceBeforeTypeAnnotation: false
};

/**
* @param {string} directoryPath
*/
Expand Down Expand Up @@ -47,7 +72,7 @@ function generateInitialDeclaration(excludeDir) {
return defaultCompilerHost.getSourceFile(fileName, languageVersion, onError, shouldCreateNewSourceFile);
}
};

const program = ts.createProgram(parsedConfig.fileNames, parsedConfig.options, customCompilerHost);
program.emit();
return fileContent;
Expand Down Expand Up @@ -280,6 +305,7 @@ function fixDeclaration(content, aceNamespacePath) {
let allReferences = referencePaths.join("\n") + "\n/// <reference path=\"./ace-modes.d.ts\" />\n";
output = allReferences + output;
}
output = formatDts(outputName, output);
fs.writeFileSync(outputName, output);
});

Expand All @@ -289,12 +315,53 @@ function fixDeclaration(content, aceNamespacePath) {

const sourceCode = program.getSourceFile(temporaryName);
const result = ts.transform(sourceCode, [transformer, pathBasedTransformer]);

result.dispose();

checkFinalDeclaration(finalDeclarations);
}

function createMinimalLanguageServiceHost() {
return {
files: {},
addFile(fileName, text) {
this.files[fileName] = ts.ScriptSnapshot.fromString(text);
},
"getCompilationSettings": function () {
return ts.getDefaultCompilerOptions();
},
"getScriptFileNames": function () {
return Object.keys(this.files);
},
"getScriptVersion": function (_fileName) {
return "0";
},
"getScriptSnapshot": function (fileName) {
return this.files[fileName];
},
"getCurrentDirectory": function () {
return "";
}
};

}

function formatDts(filename, text) {
var host = createMinimalLanguageServiceHost();
host.addFile(filename, text);
const languageService = ts.createLanguageService(host);
let formatEdits = languageService.getFormattingEditsForDocument(filename, defaultFormatCodeSettings);
formatEdits
.sort((a, b) => a.span.start - b.span.start)
.reverse()
.forEach(edit => {
const head = text.slice(0, edit.span.start);
const tail = text.slice(edit.span.start + edit.span.length);
text = `${head}${edit.newText}${tail}`;
});
return text;
}


/**
* @param {string[]} declarationNames
Expand Down Expand Up @@ -408,7 +475,7 @@ function cloneAceNamespace(aceNamespacePath) {
*/
function generateDeclaration(aceNamespacePath) {
if (!aceNamespacePath) {
aceNamespacePath = __dirname + "/../ace-internal.d.ts"
aceNamespacePath = __dirname + "/../ace-internal.d.ts";
}
const excludeDir = "src/mode"; //TODO: remove, when modes are ES6

Expand All @@ -433,7 +500,9 @@ function generateDeclaration(aceNamespacePath) {
*/
function updateDeclarationModuleNames(content) {
let output = content.replace(
/ace\-code(?:\/src)?\/(mode(?!\/(?:matching_brace_outdent|matching_parens_outdent|behaviour|folding))|theme|ext|keybinding|snippets)\//g, "ace-builds/src-noconflict/$1-");
/ace\-code(?:\/src)?\/(mode(?!\/(?:matching_brace_outdent|matching_parens_outdent|behaviour|folding))|theme|ext|keybinding|snippets)\//g,
"ace-builds/src-noconflict/$1-"
);
output = output.replace(/"ace\-code"/g, "\"ace-builds\"");
output = output.replace(/ace\-code(?:\/src)?/g, "ace-builds-internal");
return output;
Expand All @@ -442,7 +511,7 @@ function updateDeclarationModuleNames(content) {

if (!module.parent) {
require("./modes-declaration-generator");
generateDeclaration();
generateDeclaration();
}
else {
exports.generateDeclaration = generateDeclaration;
Expand Down

0 comments on commit ed40c51

Please sign in to comment.