added configuration option for Item name casing, refactoring (#133)

- the casing of generated Item names from Thing labels can
  now be configured using the extension's configuration.
- the ItemsProvider.ts file has been refactored to not
  depend on the 'underscore.strings' library any longer.

fixes #132

Signed-off-by: Willi Eggeling <thewilli@gmail.com>
pull/139/head
Willi Eggeling 2019-02-22 21:53:45 +01:00 committed by Kuba Wolanin
parent 08a9335971
commit 7f8c83b3aa
5 changed files with 55 additions and 14 deletions

View File

@ -6,6 +6,8 @@
- removed settings param 'restCompletions'
- renamed settings param 'lspEnabled' to 'remoteLspEnabled'
- renamed settings param 'lspPort' to 'remoteLspPort'
- added settings param 'itemCasing' to allow for Item format configuration (#133)
- removed reference to library 'underscore.string' (#133)
## 0.4.1 - 2018-12-09
- Fixed Basic UI Preview (#117)

View File

@ -23,7 +23,6 @@
"lodash": "^4.17.4",
"request": "^2.83.0",
"request-promise-native": "^1.0.5",
"underscore.string": "^3.3.5",
"vscode": "^1.1.26",
"vscode-languageclient": "^5.2.1"
}

View File

@ -1,23 +1,35 @@
import {
SnippetString,
window
window,
workspace
} from 'vscode'
import * as _ from 'lodash'
import * as s from 'underscore.string'
import * as AsciiTable from 'ascii-table'
import { Thing } from './Thing'
import { Channel } from './Channel'
import { humanize } from '../Utils'
/** generate an Item name from a Thing label by using the configured casing */
function generateItemName(label: string) : string {
const config = workspace.getConfiguration('openhab');
switch(config.get('itemCasing')) {
case 'snake':
// upper snake case, 'Guest Room Light' -> 'Guest_Room_Light'
return _.startCase(label).replace(/ /g, "_");
default:
// camel case, 'Guest Room Light' -> 'GuestRoomLight'
return _.startCase(label).replace(/ /g, "");
}
}
const CHANNEL_TEMPLATE = (channel: Channel): SnippetString => {
if (channel.kind === 'STATE') {
let name = s.classify(channel.id);
let label = channel.label || s(channel.id)
.replaceAll('#', ' ')
.humanize()
.value()
const name = generateItemName(channel.id);
const label = humanize(
(channel.label || channel.id).replace(/#/g, ' ')
);
return new SnippetString(
`${channel.itemType} ${name} "${label}" {channel="${channel.uid}"}\n`
)
@ -32,11 +44,10 @@ const THING_TEMPLATE = (thing: Thing): SnippetString => {
let channels = _(thing.channels)
.filter(channel => channel.kind === 'STATE')
.map((channel: Channel) => {
let name = s.classify(thing.label) + '_' + s.classify(channel.id);
let label = channel.label || s(channel.id)
.replaceAll('#', ' ')
.humanize()
.value()
const name = generateItemName(`${thing.label} ${channel.id}`);
const label = humanize(
(channel.label || channel.id).replace(/#/g, ' ')
);
return [channel.itemType, name, `"${label}"`, `{channel="${channel.uid}"}`]
})
.value()

View File

@ -10,6 +10,26 @@ import {PreviewPanel} from './WebView/PreviewPanel'
import * as _ from 'lodash'
import * as request from 'request-promise-native'
/**
* humanize function adapter from the previously included underscore.string library
*/
export function humanize(str: string) : string {
return _.upperFirst(
// original 'underscored' of underscore.string
str.trim()
.replace(/([a-z\d])([A-Z]+)/g, '$1_$2')
.replace(/[-\s]+/g, '_')
.toLowerCase()
.replace(/([a-z\d])([A-Z]+)/g, '$1_$2')
.replace(/_id$/, '')
.replace(/_/g, ' ')
// original 'humanize' of underscore.string
.replace(/_id$/, '')
.replace(/_/g, ' ')
.trim()
);
}
export function getHost() {
let config = workspace.getConfiguration('openhab')
let host = config.host

View File

@ -237,6 +237,15 @@
],
"default": "basicui",
"description": "(optional) Choose between `basicui` and `classicui` for the sitemap preview panel"
},
"openhab.itemCasing": {
"type": "string",
"default": "camel",
"enum": [
"camel",
"snake"
],
"markdownDescription": "Choose how the `Create Items from Channels` command generates Item names. Use `camel` for `CamelCase` or `snake` for `Upper_Snake_Case`."
}
}
},