summaryrefslogtreecommitdiff
path: root/src/main/completion.h
blob: 25f1ea2c55cfdbb050fe9a46f3d0f303a33f8256 (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
#ifndef _COMPLETION_H
#define _COMPLETION_H

#include <stddef.h>

#include "dged/location.h"

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

struct buffer;
struct buffers;
struct buffer_view;
struct commands;

typedef struct region (*completion_render_fn)(void *, struct buffer *);
typedef void (*completion_selected_fn)(void *, struct buffer_view *);
typedef void (*completion_cleanup_fn)(void *);

struct completion {
  void *data;
  completion_render_fn render;
  completion_selected_fn selected;
  completion_cleanup_fn cleanup;
};

typedef void (*add_completions)(struct completion *, size_t);

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

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

  /** Callback for adding items to the completion list */
  add_completions add_completions;
};

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

typedef void (*provider_cleanup_fn)(void *);

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

  /** Completion function. Called to trigger retreival of new completions. */
  completion_fn complete;

  /** Cleanup function called when provider is destroyed. */
  provider_cleanup_fn cleanup;

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

/**
 * Initialize the completion system.
 *
 */
void init_completion(struct buffers *buffers);

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

/**
 * Enable completions in the buffer @p source.
 *
 * @param source [in] The buffer to provide completions for.
 * @param providers [in] The completion providers to use.
 * @param nproviders [in] The number of providers in @p providers.
 */
void add_completion_providers(struct buffer *source,
                              struct completion_provider *providers,
                              uint32_t nproviders);

/**
 * Trigger a completion at @ref at in @ref buffer.
 *
 * @param buffer [in] Buffer to complete in.
 * @param at [in] The location in @ref buffer to provide completions at.
 */
void complete(struct buffer *buffer, struct location at);

/**
 * 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);

/**
 * Get a pointer to the buffer used to hold completion items.
 *
 * @returns A pointer to the buffer holding completions.
 */
struct buffer *completion_buffer(void);

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

void pause_completion();
void resume_completion();

#endif