forked from jlandersen/vscode-kafka
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide documentation on hover, in .kafka files
Fixes jlandersen#149 Signed-off-by: azerr <[email protected]>
- Loading branch information
1 parent
7191910
commit ae53c98
Showing
6 changed files
with
243 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
import { Hover, Position, Range, TextDocument } from "vscode"; | ||
import { createTopicDocumentation, SelectedClusterProvider, TopicProvider } from "../kafkaFileLanguageService"; | ||
import { consumerModel, Model, producerModel } from "../model"; | ||
import { Block, BlockType, Chunk, ConsumerBlock, KafkaFileDocument, MustacheExpression, NodeKind, ProducerBlock, Property } from "../parser/kafkaFileParser"; | ||
|
||
export class KafkaFileHover { | ||
|
||
constructor(private selectedClusterProvider: SelectedClusterProvider, private topicProvider: TopicProvider) { | ||
|
||
} | ||
|
||
async doHover(document: TextDocument, kafkaFileDocument: KafkaFileDocument, position: Position): Promise<Hover | undefined> { | ||
// Get the AST node before the position where complation was triggered | ||
const node = kafkaFileDocument.findNodeAt(position); | ||
if (!node) { | ||
return; | ||
} | ||
switch (node.kind) { | ||
|
||
case NodeKind.consumerBlock: { | ||
const block = <Block>node; | ||
const topic = block.getPropertyValue('topic'); | ||
return new Hover( | ||
'Consumer declaration for the topic `' + topic + | ||
'`. See [here](https://github.com/jlandersen/vscode-kafka/blob/master/docs/Consuming.md#kafka-file) for more informations.', | ||
node.range()); | ||
} | ||
|
||
case NodeKind.producerBlock: { | ||
const block = <Block>node; | ||
const topic = block.getPropertyValue('topic'); | ||
return new Hover( | ||
'Producer declaration for the topic `' + topic + | ||
'`. See [here](https://github.com/jlandersen/vscode-kafka/blob/master/docs/Producing.md#kafka-file) for more informations.', | ||
node.range()); | ||
} | ||
|
||
case NodeKind.propertyKey: { | ||
const propertyKey = <Chunk>node; | ||
const property = <Property>propertyKey.parent; | ||
const propertyName = propertyKey.content; | ||
const propertyKeyRange = propertyKey.range(); | ||
const block = <Block>property.parent; | ||
if (block.type === BlockType.consumer) { | ||
// CONSUMER | ||
// key|: | ||
|
||
// or | ||
|
||
// CONSUMER | ||
// key| | ||
return await this.getHoverForConsumerPropertyNames(propertyName, propertyKeyRange, <ConsumerBlock>block); | ||
} else { | ||
// PRODUCER | ||
// key|: | ||
return await this.getHoverForProducerPropertyNames(propertyName, propertyKeyRange, <ProducerBlock>block); | ||
} | ||
} | ||
|
||
case NodeKind.propertyValue: { | ||
const propertyValue = <Chunk>node; | ||
const property = <Property>propertyValue.parent; | ||
const block = <Block>property.parent; | ||
if (block.type === BlockType.consumer) { | ||
// CONSUMER | ||
// key-format: | | ||
return await this.getHoverForConsumerPropertyValues(propertyValue, property, <ConsumerBlock>block); | ||
} else { | ||
// PRODUCER | ||
// key-format: | | ||
return await this.getHoverForProducerPropertyValues(propertyValue, property, <ProducerBlock>block); | ||
} | ||
} | ||
|
||
case NodeKind.mustacheExpression: { | ||
const expression = <MustacheExpression>node; | ||
return new Hover('FakerJS expression. See [here](https://github.com/jlandersen/vscode-kafka/blob/master/docs/Producing.md#randomized-content) for more informations.', expression.enclosedExpressionRange); | ||
} | ||
|
||
case NodeKind.producerValue: { | ||
return new Hover('Producer value. See [here](https://github.com/jlandersen/vscode-kafka/blob/master/docs/Producing.md#kafka-file) for more informations.', node.range()); | ||
} | ||
} | ||
} | ||
|
||
async getHoverForConsumerPropertyNames(propertyName: string, propertyKeyRange: Range, block: ConsumerBlock): Promise<Hover | undefined> { | ||
return await this.getHoverForPropertyNames(propertyName, propertyKeyRange, block, consumerModel); | ||
} | ||
|
||
async getHoverForProducerPropertyNames(propertyName: string, propertyKeyRange: Range, block: ProducerBlock): Promise<Hover | undefined> { | ||
return await this.getHoverForPropertyNames(propertyName, propertyKeyRange, block, producerModel); | ||
} | ||
|
||
async getHoverForPropertyNames(propertyName: string, propertyKeyRange: Range, block: Block, metadata: Model): Promise<Hover | undefined> { | ||
const definition = metadata.getDefinition(propertyName); | ||
if (definition && definition.description) { | ||
return new Hover(definition.description, propertyKeyRange); | ||
} | ||
} | ||
|
||
async getHoverForConsumerPropertyValues(propertyValue: any, property: Property, block: ConsumerBlock): Promise<Hover | undefined> { | ||
const propertyName = property.propertyName; | ||
switch (propertyName) { | ||
case 'topic': | ||
// CONSUMER | ||
// topic: | | ||
return await this.getHoverForTopic(property); | ||
default: | ||
// CONSUMER | ||
// key-format: | | ||
return await this.getHoverForPropertyValues(propertyValue, property, block, consumerModel); | ||
} | ||
} | ||
|
||
|
||
async getHoverForProducerPropertyValues(propertyValue: any, property: Property, block: ProducerBlock): Promise<Hover | undefined> { | ||
const propertyName = property.propertyName; | ||
switch (propertyName) { | ||
case 'topic': | ||
// PRODUCER | ||
// topic: | | ||
return await this.getHoverForTopic(property); | ||
default: | ||
// PRODUCER | ||
// key-format: | | ||
return await this.getHoverForPropertyValues(propertyValue, property, block, producerModel); | ||
} | ||
} | ||
|
||
async getHoverForTopic(property: Property): Promise<Hover | undefined> { | ||
const propertyValue = property.value; | ||
if (!propertyValue) { | ||
return; | ||
} | ||
const { clusterId } = this.selectedClusterProvider.getSelectedCluster(); | ||
if (!clusterId) { | ||
return; | ||
} | ||
|
||
try { | ||
const topicId = propertyValue.content.trim(); | ||
const topics = await this.topicProvider.getTopics(clusterId); | ||
if (topics.length > 0) { | ||
const topic = topics | ||
.find(t => t.id === topicId); | ||
if (topic) { | ||
return new Hover(createTopicDocumentation(topic), propertyValue.range()); | ||
} | ||
} | ||
} | ||
catch (e) { | ||
return; | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
async getHoverForPropertyValues(propertyValue: Chunk | undefined, property: Property, block: Block, metadata: Model): Promise<Hover | undefined> { | ||
const propertyName = property.propertyName; | ||
if (!propertyName) { | ||
return; | ||
} | ||
const definition = metadata.getDefinition(propertyName); | ||
if (definition && !definition.description) { | ||
return new Hover(definition.description, propertyValue?.range()); | ||
} | ||
return undefined; | ||
} | ||
|
||
} |