|
1 |
| -export interface KotlinConstants { |
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2020 Google Inc. All rights reserved. |
| 4 | + * This code may only be used under the BSD style license found at |
| 5 | + * http://polymer.github.io/LICENSE.txt |
| 6 | + * Code distributed by Google as part of this project is also |
| 7 | + * subject to an additional IP rights grant found at |
| 8 | + * http://polymer.github.io/PATENTS.txt |
| 9 | + */ |
| 10 | + |
| 11 | +/** |
| 12 | + * Kotlin language formatting preferences. |
| 13 | + */ |
| 14 | +export interface KotlinPreferences { |
2 | 15 | indent: number;
|
3 | 16 | lineLength: number;
|
4 | 17 | }
|
5 | 18 |
|
6 |
| -export const KT_BASE: KotlinConstants = {indent: 4, lineLength: 120}; |
| 19 | +/** |
| 20 | + * Default language formatting settings. |
| 21 | + */ |
| 22 | +export const KT_DEFAULT: KotlinPreferences = {indent: 4, lineLength: 120}; |
7 | 23 |
|
8 |
| -function collectionTemplate(items: string[], indent: number = KT_BASE.indent, collection: string = 'list') { |
9 |
| - const lowered = collection.toLowerCase(); |
10 |
| - if (items.length == 0){ |
11 |
| - return `empty${lowered[0].toUpperCase()}${lowered.substring(1)}()`; |
| 24 | +/** |
| 25 | + * Collection of utilities for generating Kotlin code. |
| 26 | + */ |
| 27 | +export class KotlinGenerationUtils { |
| 28 | + constructor(public pref: KotlinPreferences = KT_DEFAULT) { |
12 | 29 | }
|
13 |
| - return `${lowered}Of(${joinWithinLine(items, indent)})`; |
14 |
| -} |
15 | 30 |
|
16 |
| -export function mapOf(items: string[], indent: number = KT_BASE.indent): string { |
17 |
| - return collectionTemplate(items, indent, 'map'); |
18 |
| -} |
| 31 | + /** |
| 32 | + * Formats a function application in Kotlin. |
| 33 | + * |
| 34 | + * @param name name of the function |
| 35 | + * @param args list of arguments to the function |
| 36 | + * @param emptyName alternative name for the function with empty arguments. |
| 37 | + */ |
| 38 | + applyFun(name: string, args: string[], emptyName: string = name): string { |
| 39 | + if (args.length == 0) return `${emptyName}()`; |
| 40 | + return `${name}(${this.joinWithIndents(args)})`; |
| 41 | + } |
19 | 42 |
|
20 |
| -export function listOf(items: string[], indent: number = KT_BASE.indent): string { |
21 |
| - return collectionTemplate(items, indent); |
22 |
| -} |
| 43 | + /** Formats `mapOf` with correct indentation and defaults. */ |
| 44 | + mapOf(args: string[]): string { |
| 45 | + return this.applyFun('mapOf', args, 'emptyMap'); |
| 46 | + } |
| 47 | + |
| 48 | + /** Formats `mutableMapOf` with correct indentation and defaults. */ |
| 49 | + mutableMapOf(args: string[]): string { |
| 50 | + return this.applyFun('mutableMapOf', args); |
| 51 | + } |
23 | 52 |
|
24 |
| -export function setOf(items: string[], indent: number = KT_BASE.indent): string { |
25 |
| - return collectionTemplate(items, indent, 'set'); |
| 53 | + /** Formats `listOf` with correct indentation and defaults. */ |
| 54 | + listOf(args: string[]): string { |
| 55 | + return this.applyFun('listOf', args, 'emptyList'); |
| 56 | + } |
| 57 | + |
| 58 | + /** Formats `setOf` with correct indentation and defaults. */ |
| 59 | + setOf(args: string[]): string { |
| 60 | + return this.applyFun('setOf', args, 'setList'); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Joins a list of items, taking line length and indentation into account. |
| 65 | + * |
| 66 | + * @param items strings to join |
| 67 | + * @param lineStart (optional) add the starting indentation when calculating line limits. |
| 68 | + */ |
| 69 | + joinWithIndents(items: string[], lineStart: number = 0): string { |
| 70 | + const candidate = items.join(', '); |
| 71 | + if (lineStart + candidate.length <= this.pref.lineLength) return candidate; |
| 72 | + return '\n' + leftPad(leftPad(items.join(',\n'), this.pref.indent), lineStart) + '\n'; |
| 73 | + } |
26 | 74 | }
|
27 | 75 |
|
| 76 | + |
| 77 | +/** Everyone's favorite NPM module, install not required. */ |
28 | 78 | export function leftPad(input: string, indent: number, skipFirst: boolean = false) {
|
29 | 79 | return input
|
30 | 80 | .split('\n')
|
31 | 81 | .map((line: string, idx: number) => (idx === 0 && skipFirst) ? line : ' '.repeat(indent) + line)
|
32 | 82 | .join('\n');
|
33 | 83 | }
|
34 | 84 |
|
35 |
| -export function joinWithinLine(items: string[], |
36 |
| - startIndent: number, |
37 |
| - indent: number = KT_BASE.indent, |
38 |
| - limit: number = KT_BASE.lineLength): string { |
39 |
| - const candidate = items.join(', '); |
40 |
| - if (startIndent + candidate.length <= limit) return candidate; |
41 |
| - return '\n' + leftPad(items.join(',\n' + indent), startIndent) + '\n'; |
42 |
| -} |
|
0 commit comments