<template>
  <codemirror
      v-model="javaCode"
      :placeholder="editorPlaceholder"
      :style="{ height: editorHeight+'px' }"
      :autofocus="true"
      :indent-with-tab="true"
      :tabSize="tabSize"
      :extensions="extensions"
      :scrollbarStyle="null"
  />
</template>

<script setup>
import {Codemirror} from "vue-codemirror";
import {java, javaLanguage} from "@codemirror/lang-java";

const emit = defineEmits()
const props = defineProps({
  value: {
    type: String,
    default: "",
  },
  editorPlaceholder: {
    type: String,
    default: "请输入代码",
  },
  editorHeight: {
    type: String,
    default: "300",
  },
  tabSize: {
    type: Number,
    default: 2,
  }
})
const _value = computed({
  get() {
    return props.value || ""
  },
  set(value) {
    emit('update:value', value)
  }
})
const javaCode = ref();
const customJavaCodeTips = (context) => {
  let word = context.matchBefore(/\w*/)
  if (word.from == word.to && !context.explicit) return null;
  return {
    from: word.from?word.from:context.pos,
    options: [
      {label: "function", type: "keyword",apply:"function name(params) {\n\t\n}",detail: "definition"},
      // {label: "hello", type: "variable", info: "(World)"},
      {label: "fori", type: "keyword",apply:"for (let i = 0; i < list.length; i++) {\n\n}", detail: "loop"},
      {label: "for", type: "keyword",apply:"for (let name of list) {\n\n}", detail: "of loop"},
      {label: "psf", type: "keyword",apply:"public static final ", detail: "public static final"},
      {label: "psv", type: "keyword",apply:"public static void name(){\n\n}", detail: "public static void"},
      {label: "psfi", type: "keyword",apply:"public static final int ", detail: "public static final int"},
      {label: "psfs", type: "keyword",apply:"public static final String ", detail: "public static final String"},
      {label: "psvm", type: "keyword", apply: "public static void main(String[] args){\n\t\n}", detail: "main() 方法声明"}
    ]
  }
}

const javaSnippets = javaLanguage.data.of({
  autocomplete: customJavaCodeTips,
})
const extensions = ref([java(), javaSnippets]);
</script>
