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

Quick Fix Import Button #12051

Merged
merged 8 commits into from
Jan 20, 2025
Merged
Changes from 6 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
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
<script setup lang="ts">
import SvgButton from '@/components/SvgButton.vue'
import SvgIcon from '@/components/SvgIcon.vue'
import { useGraphStore } from '@/stores/graph'
import { QualifiedImport } from '@/stores/graph/imports'
import type { Icon } from '@/util/iconMetadata/iconName'
import { QualifiedName } from '@/util/qualifiedName'

const graph = useGraphStore()

const props = defineProps<{
message: string
type: MessageType
}>()

function containsLibraryName(): string | null {
const prefix = 'Compile error: Fully qualified name references a library '
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this message somewhere as constant? Seems a bit dangerous to hardcode it like this.

Copy link
Member Author

@JaroslavTulach JaroslavTulach Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be a constant, but in Java code. We don't have a way to share constants between Java and JavaScript. However you are right. This is slightly fragile, in the long run we could try:

Copy link
Member Author

@JaroslavTulach JaroslavTulach Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's enhance the language server protocol we have. For example let's extend Diagnostic with possible hints, like:

hint: {
  type: "import",
  add: "Standard.Table"
}

that way we would formalize this API - would be better in the long run then depending on the text of the message.

if (props.message.startsWith(prefix)) {
const rest = props.message.substring(prefix.length)
const libName = rest.split(' ')
return libName[0] ? libName[0] : null
} else {
return null
}
}
function copyText() {
window.navigator.clipboard.writeText(props.message)
}
function fixImport() {
const libName = containsLibraryName()
if (typeof libName == `string`) {
const theImport = {
kind: 'Qualified',
module: libName as QualifiedName,
} as QualifiedImport
const edit = graph.startEdit()
graph.addMissingImports(edit, [theImport])
graph.commitEdit(edit)
}
}
</script>

<script lang="ts">
Expand All @@ -22,6 +49,7 @@ export const iconForMessageType: Record<MessageType, Icon> = {
missing: 'metadata',
panic: 'panic',
}

export const colorForMessageType: Record<MessageType, string> = {
error: 'var(--color-error)',
warning: 'var(--color-warning)',
Expand All @@ -35,7 +63,20 @@ export const colorForMessageType: Record<MessageType, string> = {
<SvgIcon class="icon" :name="iconForMessageType[props.type]" />
<div class="message" v-text="props.message"></div>
<div class="toolbar">
<SvgButton name="copy2" class="copyButton" title="Copy message text" @click.stop="copyText" />
<SvgButton
v-if="containsLibraryName()"
name="edit"
class="copyButton"
title="Fix Import"
@click.stop="fixImport"
/>
<SvgButton
v-if="!containsLibraryName()"
name="copy2"
class="copyButton"
title="Copy message text"
@click.stop="copyText"
/>
</div>
</div>
</template>
Expand Down
Loading