Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User name auto complete on certain commands #32

Merged
merged 5 commits into from
Oct 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Unreleased

### 🚀 New Feature

- When autocompleting a username argument of a command, only usernames will now be suggested ([#32](https://github.com/HiDeoo/YaTA/pull/32) - [nD00rn](https://github.com/nD00rn)).

# 1.11.0

### 🚀 New Feature
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
"bugs": {
"url": "https://github.com/HiDeoo/YaTA/issues"
},
"contributors": [],
"contributors": [
"nDoorn <ndoorn@outlook.com>"
],
"homepage": "https://yata.now.sh"
}
2 changes: 1 addition & 1 deletion src/components/Input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export default class Input extends React.Component<Props, State> {

this.completions = this.props.getCompletions(
word,
previousCharacter === '@',
previousCharacter === '@' || Command.isUsernameCompletableCommandArgument(text, cursor),
start === 1 && Command.isCommand(previousCharacter)
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/constants/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export type CommandDescriptor = {
/**
* A command argument.
*/
type CommandArgument = {
export type CommandArgument = {
name: string
optional?: boolean
}
53 changes: 52 additions & 1 deletion src/libs/Command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as _ from 'lodash'

import { CommandDescriptor, CommandName, Commands } from 'Constants/command'
import { CommandArgument, CommandDescriptor, CommandName, Commands } from 'Constants/command'
import Notice from 'Libs/Notice'
import { SerializedRoomState } from 'Libs/RoomState'
import Twitch from 'Libs/Twitch'
Expand Down Expand Up @@ -44,6 +44,57 @@ export default class Command {
return /^[\/|.]help(?:$|\s)/i.test(message)
}

/**
* Checks if the cursor in a message is associated to a command username auto-completable argument.
* Note: at the moment, it is assumed that all commands having a username completable argument have that argument at
* the first position.
* @param message - The message.
* @param cursor - The cursor position.
* @return `true` when the cursor is matching a username completable argument.
*/
public static isUsernameCompletableCommandArgument(message: string, cursor: number) {
// Bail out if the message is not even a command.
if (!Command.isCommand(message)) {
return false
}

// Grab the command name and the first argument.
const words = message.split(' ')
const commandName = words[0].substr(1)
const firstArgument = words[1]

// If we don't have a first argument yet, bail out.
if (_.isNil(firstArgument)) {
return false
}

const firstArgumentStart = commandName.length + 2
const firstArgumentEnd = firstArgumentStart + firstArgument.length

// If we're not auto-completing the first agument, bail out.
if (cursor < firstArgumentStart || cursor > firstArgumentEnd) {
return false
}

// Get the descriptor for this command.
const descriptor = Command.getDescriptor(commandName)

// If we can't get a valid descriptor, this means the command is unknown.
if (_.isNil(descriptor.name)) {
return false
}

const firstArgumentDescriptor = _.get(descriptor, 'arguments[0]') as Optional<CommandArgument>

// If the command doesn't accept any argument or the first argument is not a
// username, bail out.
if (_.isNil(firstArgumentDescriptor) || firstArgumentDescriptor.name !== 'username') {
return false
}

return true
}

/**
* Parses a message as a whisper command (/w user message).
* @return The whisper details.
Expand Down