summaryrefslogtreecommitdiff
path: root/src/main/completion.h
blob: f2ce18661a81e51459961a52fa6960001eb99e7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#ifndef _COMPLETION_H
#define _COMPLETION_H

#include "dged/location.h"

/** @file completion.h
 * Auto-complete system.
 */

struct buffer;
struct buffers;
struct commands;

/**
 * A single completion.
 */
struct completion {
  /** The display text for the completion. */
  const char *display;

  /** The text to insert for this completion. */
  const char *insert;

  /**
   * True if this completion item represent a fully expanded value.
   *
   * One example might be when the file completion represents a
   * file (and not a directory) which means that there is not
   * going to be more to complete after picking this completion
   * item.
   */
  bool complete;
};

/**
 * Context for calculating completions.
 */
struct completion_context {
  /** The buffer to complete in. */
  struct buffer *buffer;

  /** The current location in the buffer. */
  const struct location location;

  /** The capacity of @ref completion_context.completions. */
  const uint32_t max_ncompletions;

  /** The resulting completions */
  struct completion *completions;
};

/**
 * A function that provides completions.
 */
typedef uint32_t (*completion_fn)(struct completion_context ctx,
                                  void *userdata);

/**
 * A completion provider.
 */
struct completion_provider {
  /** Name of the completion provider */
  char name[16];

  /** Completion function. Called to get new completions. */
  completion_fn complete;

  /** Userdata sent to @ref completion_provider.complete */
  void *userdata;
};

/**
 * Type of event that triggers a completion.
 */
enum completion_trigger_kind {
  /** Completion is triggered on any input. */
  CompletionTrigger_Input = 0,

  /** Completion is triggered on a specific char. */
  CompletionTrigger_Char = 1,
};

/**
 * Description for @c CompletionTrigger_Input.
 */
struct completion_trigger_input {
  /** Trigger completion after this many chars */
  uint32_t nchars;

  /** Trigger an initial complete? */
  bool trigger_initially;
};

/**
 * Completion trigger descriptor.
 */
struct completion_trigger {
  /** Type of trigger. */
  enum completion_trigger_kind kind;
  union completion_trigger_data {
    uint32_t c;
    struct completion_trigger_input input;
  } data;
};

/**
 * Initialize the completion system.
 *
 * @param buffers The buffer list to complete from.
 * @param commands The command list to complete from.
 */
void init_completion(struct buffers *buffers, struct commands *commands);

/**
 * Tear down the completion system.
 */
void destroy_completion(void);

/**
 * Callback for completion inserted.
 */
typedef void (*insert_cb)(void);

/**
 * Enable completions in the buffer @p source.
 *
 * @param source [in] The buffer to provide completions for.
 * @param trigger [in] The completion trigger to use for this completion.
 * @param providers [in] The completion providers to use.
 * @param nproviders [in] The number of providers in @p providers.
 * @param on_completion_inserted [in] Callback to be called when a completion
 * has been inserted.
 */
void enable_completion(struct buffer *source, struct completion_trigger trigger,
                       struct completion_provider *providers,
                       uint32_t nproviders, insert_cb on_completion_inserted);

/**
 * Create a new path completion provider.
 *
 * This provider completes filesystem paths.
 * @returns A filesystem path @ref completion_provider.
 */
struct completion_provider path_provider(void);

/**
 * Create a new buffer completion provider.
 *
 * This provider completes buffer names from the
 * buffer list.
 * @returns A buffer name @ref completion_provider.
 */
struct completion_provider buffer_provider(void);

/**
 * Create a new command completion provider.
 *
 * This provider completes registered command names.
 * @returns A command name @ref completion_provider.
 */
struct completion_provider commands_provider(void);

/**
 * Abort any active completion.
 */
void abort_completion(void);

/**
 * Is a completion currently showing?
 *
 * @returns True if the completion window is showing completions.
 */
bool completion_active(void);

/**
 * Disable completion for @ref buffer.
 *
 * @param buffer [in] Buffer to disable completions for.
 */
void disable_completion(struct buffer *buffer);

#endif